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.
This has nothing to do with boxing or unboxing.

long myLarger = 500000; 
int mySmaller;
mySmaller = (int)myLarger;

18) 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

19) Can you describe Unboxing and provide an example ?
System.Object data types can be converted to value data types. This is called Unboxing
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

20) 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"; 

21) Can you define a String constant that can be accessed anywhere ?

public class MyConstants 
{
   static const string MYTEXT = "hello";
}

22) What is an Enumeration ?
An Enumeration provides a way of grouping symbolic constants.
The default data type is System.Int32.
The first item in an enumeration has a value of 0.
An enumeration is a value data type.
The default access modifier for an enumeration is public.

public enum enMyCities 
{
    london = 1,
    tokyo = 2
}

23) Is it possible to return the string names instead of the numbers from an Enumeration ?
Yes. You can use the enumeration GetName method.

int myValue = enMyCities.tokyo; 
string MyText = System.Enum.GetName(typeof(enMyCities), myValue);

24) Write code to iterate through the values defined in an Enumeration ?
The Enum base class does not support the IEnumerable interface.
However you can use the GetValues method which returns all the enumerated values in an array.

foreach (enMyCities city in System.Enum.GetValues(typeof(enMyCities))) 
{
    Console.WriteLine( {0} - ( {1} ), city, (int)city);
}

25) Is it possible to iterate through the names defined in an Enumeration ?
Yes.

foreach (string name in System.Enum.GetNames(typeof(enMyCities))) 
{
    Console.WriteLine( {0} - ( {1} ), text, (int)System.Enum.Parse(typeof(enMyCities), text));
}

26) Is it possible to associate a text string with each enumeration value ?
Yes. You can ammend the Description component to each value.

public enum enMyCities 
{
    [System.ComponentModel.Description("UK")]
    london = 1,
    [System.ComponentModel.Description("JAPAN")]
    tokyo = 2
}

27) Write code to iterate through the values defined in an Enumeration ?
First create an Extension Method that can easily return the Description.
This extension method will use reflection to get an array of the enum's members, and then from the array's first element's member info it will check and return if the member has a Description attribute defined.
If that attribute exists, then it will return the Description value, this will be contained within the GenericEnum variable's ToString() method.

public static class EnumExtensionMethods 
{
    public static string GetDescription(this Enum GenericEnum)
    {
        Type genericEnumType = GenericEnum.GetType();
        MemberInfo[] memberInfo = genericEnumType.GetMember(GenericEnum.ToString());
        if ((memberInfo != null && memberInfo.Length > 0))
        {
            var _Attribs = memberInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
            if ((_Attribs != null && _Attribs.Count() > 0))
            {
                return ((System.ComponentModel.DescriptionAttribute)_Attribs.ElementAt(0)).Description;
            }
        }
        return GenericEnum.ToString();
    }
}

foreach (enMyCities city in System.Enum.GetValue(typeof(enMyCities)))
{
    string enumName = System.Enum.GetName(typeof(enMyCities), myValue);
    string enumDescription = city.GetDescription();
    Console.WriteLine( {0} - ( {1} ), enumName, enumDescription);
}

28) 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");

29) 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);


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext