User FAQs
If you have a question, please send it to us.
1) What is the difference between Value data types and Reference data types ?
A value type is also known as a primitive or simple type.
When a value data type is created, a single space in memory is allocated on the stack and is accessed directly.
Value data types are automatically given a default value of zero (or equivalent) when they are declared.
A reference type is also known as an object type.
When a reference data type is created, memory is allocated on the heap and is not accessed directly. Instead a pointer to the memory location is used.
Reference data types are automatically given a default value of null when they are declared.
2) Can you explain the difference between the Stack and the Heap ?
The stack contains value data types on a first in, first out basis.
The heap contains reference data types.
3) Can you give some examples of Value data types ?
struct
enum
System.Byte // integer 0 to 255
System.Decimal
System.Double
System.Single
System.Int16
System.Int32
System.Int64
System.DateTime
System.Windows.Forms.Point // structure
System.Drawing.Rectangle // structure
System.Drawing.Color // structure
4) Can you give some examples of Reference data types ?
class
interface
System.String
System.Text.StringBuilder
System.Object
System.Delegate
System.Array
System.Tuple
5) What .NET data types do the following keywords map to ?
string // System.String - Reference Data Type
short // System.Int16
int // System.Int32
long // System.Int64
float // System.Single
decimal // System.Decimal
double // System.Double
int[] // System.Int32[]
6) Can you identify the underlying .NET data type of a variable or object at run-time ?
Yes. The run-time data type can be found using the GetType method.
int myNumber;
if (myNumber.GetType().FullName == "System.Int32")
7) Does the GetType method return the underlying data type when used with COM Interop ?
No. You cannot use the GetType method in conjunction with COM Interop.
The GetType method will return "System._ComObject" for all COM Interop objects.
Excel.Range myRange;
System.Windows.MessageBox.Show(myRange.GetType().FullName);
8) Can you identify the underlying COM Interop data type of a variable or object at run-time ?
Yes. The run-time data type can be found using either the 'is' operator or the 'as' operator.
if (myObject is Excel.Range)
Excel.Range myRange = myObject as Excel.Range
if (myRange != null)
9) What is the typeof Operator ?
You can use the typeof Operator can be used at compile-time to return the System.Type object for a particular data type keyword.
This operator works with both value and reference data types.
System.Type MyType1 = typeof(System.Int32);
System.Type MyType2 = typeof(int);
System.Int32 myInteger = 10;
System.String myString = "";
System.Array myArray = new int[] {1,2};
if (myInteger.GetType() == MyType1) )
if (myString.GetType() == typeof(string) )
if (myArray.GetType() == typeof(System.Array) )
10) Is there any difference between the following three lines of code ?
Yes. There is a subtle difference because the Name property does not include the full namespace.
int myValueType = 20;
MessageBox.Show(myValueType.GetType().Name) // Int32
MessageBox.Show(myValueType.GetType().FullName) // System.Int32
MessageBox.Show(myValueType.GetType().ToString()) // System.Int32
11) Is there any difference between the following three lines of code ?
Yes. There is a subtle difference because the Name property does not include the full namespace.
string[] myReferenceType = new string[] {"Mon", "Tue", "Wed"};
MessageBox.Show(myReferenceType.GetType().Name) // String
MessageBox.Show(myReferenceType.GetType().FullName) // System.String[]
MessageBox.Show(myReferenceType.GetType().ToString()) // System.String[]
12) What is a Literal Number Suffix ?
Also known as numeric literal suffixes.
In most cases the compiler is able to determine the correct data type for a numerical literal however there might be times when the default data type is different to the desired one.
In these cases a suffix can be added to explicitly tell the compiler which data type you want to use.
myNumber = 10000000F // float = System.Single
myNumber = 10000000U // uint = System.UInt32
myNumber = 10000000L // long = System.Int64
myNumber = 10000000UL // ulong = System.UInt64
myNumber = 10000000M // decimal = System.Decimal
13) What are Nullable data types ?
These are Value data types that can be assigned a null value.
This does not affect the default value.
They represent all the values of their underlying data type plus the additional null value.
int? myNumber = null;
System.Nullable<int> myNumber = null;
Each instance of a nullable data type has two public read-only properties.
HasValue - returns true if the variable contains a non-null value.
Value - should only be accessed when HasValue is true.
int? myNumber = null;
myNumber = 50
if (myNumber.HasValue == true)
{
MessageBox.Show(myNumber.Value);
}
14) Can you explain the following lines of code ?
int? myNumber = null;
long myReturn;
myReturn = myNumber ?? 20.5;
An integer variable is declared with an initial value of null.
A long variable is declared with an initial value of zero.
The long variable is then assigned the value 20.5 because the integer variable has a value of null.
The nullable coalescing operator (??) can be used to provide an alternative value when a variable is null.
This operator works with both value and reference data types.
15) Can you explain the following lines of code ?
System.Array myArray;
long myReturn;
myReturn = myArray ?? 20.5;
An array variable is declared with an initial default value of null.
A long variable is declared with an initial default value of zero.
The long variable is then assigned the value 20.5 because the myArray variable is null.
16) Can you explain the following lines of code ?
int myNumber = 10;
myNumber += DateTime.Today.Day;
MessageBox.Show(myNumber);
An integer variable is declared and is assigned the value 10.
The addition self-assignment operator (+=) adds the numerical day of the month to the value 10.
This total is then displayed in a message box.
17) What is Casting ?
Casting is the name used to describe an explicit data type conversion.
When one data type is explicitly converted to another data type.
This has nothing to do with boxing or unboxing.
long myLong = 500000;
int myInteger;
myInteger = System.Convert.ToInt32(myLong);
18) Can you describe Unboxing and provide an example ?
System.Object data types can be converted to value data types. This is called Unboxing
This is a specific type of casting.
Converting a reference type into a value type is called Unboxing
object myObject = 10; // implicit boxing
int myNumber = (int)myObject; // explicit unboxing
int myNumber = myObject as int; // explicit unboxing
int myNumber = System.Convert.ToInt32(myObject); // explicit unboxing
19) Can you describe Boxing and provide an example ?
Value data types can be converted to objects. This is called Boxing
Boxing is encapsulating a copy of a value data type inside a System.Object.
When a value data type is boxed it is allocated on the heap and not on the stack.
int myNumber = 5;
object MyObject1 = myNumber; // implicit boxing
object MyObject2 = (object)myNumber; // explicit boxing
object MyObject3 = myNumber as object; // explicit boxing
int myInt = 5;
System.Collections.ArrayList arrList = new System.Collections.ArrayList();
arrList.Add(myInt); // implicit boxing
20) What are the differences between Int32.Parse() and Convert.Int32() ?
Int32.Parse can only be used to convert strings.
Int32.Parse will throw an exception if the arguments is null (Convert.Int32 will return zero).
21) What is the difference between a Literal constant and a Symbolic constant ?
A literal constant is a specific value such as a number, boolean, date, text.
bool myBoolean = false;
A symbolic constant is a literal constant that is represented by a name.
public const string myText = "text";
22) Can you define a String constant that can be accessed anywhere ?
public class MyConstants
{
static const string MYTEXT = "hello";
}
23) What is an Immutable data type ?
This is a type of object whose value or state cannot be changed after it has been created.
The System.String is an immutable data type.
Every time the value is modified a new instance is created.
System.String myText = "some text";
myText = myText.Replace("e","a");
24) Describe the System.Tuple data type ?
This is a data structure that has a specific number of elements and a sequence.
This data type directly supports up to seven elements.
This is a reference data type.
System.Tuple <int, string, bool> MyTuple;
MyTuple = new System.Tuple<int, string, bool>(0, "text", false);
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext