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) What is the difference between double and decimal ?
double uses 64 bits, decimal uses 128 bits
double can contain some value value +0 -0 +infinity -infinity NaN
For money always use decimal.


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

8) What is 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);

9) 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")

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

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

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

13) 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; 
Console.WriteLine(myValueType.GetType().Name) // Int32
Console.WriteLine(myValueType.GetType().FullName) // System.Int32
Console.WriteLine(myValueType.GetType().ToString()) // System.Int32

14) 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"}; 
Console.WriteLine(myReferenceType.GetType().Name) // String
Console.WriteLine(myReferenceType.GetType().FullName) // System.String[]
Console.WriteLine(myReferenceType.GetType().ToString()) // System.String[]

15) Can you describe Implicit Type Conversion ?
Also called Type Coercion.
When one data type is implicitly converted to another data type.

short myShort = 100; 
int myInteger;
myInteger = myShort;

16) Can you describe Explicit Type Conversion ?
Also called Type Casting.
When one data type is explicitly converted to another data type.

long myLong = 500000; 
int myInteger;
myInteger = (int)myLong;
myInteger = System.Convert.ToInt32(myLong);

17) Can you explain the following lines of code ?

int myNumber = 10; 
myNumber += DateTime.Today.Day;
Console.WriteLine(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.


18) Can you describe Boxing and provide an example ?
Value data types can be converted to System.Object data types. This is called Boxing
Boxing is encapsulating a copy of a value data type inside a System.Object reference type.
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
Unboxing is converting an System.Object reference type into a value data type.

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 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 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)
{
   System.Console.WriteLine(myNumber.Value);
}

24) 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.


25) 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.


26) What is the 'using' statement ?
The using block is used to obtain a resource and use it and then automatically dispose of it at the end of the block.
This allows you to not specify the data type at declaration ?



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