Preparing .NET Interview - Part 2 (Basic Types)

I am here to continue series related to .NET interview preparation. Today, we will discuss the questions related to .NET types and present the answers in easy way.

In case, you didn’t have a look at our first post, you can go through the following:

So, let’s take questions one by one.

Question 1: What’s the value and reference types? Also explain Boxing and Unboxing by an example.

Answer:

  • Value types are the types which directly store the data. Value types are stored in stack.

  • Most primitive types such as int, float and char are the example of value types.

  • On the other side, reference types are the one which stores the reference or memory address in it and the corresponding data gets stored in heap. Examples of reference types includes class, string, array and object.

  • Boxing is the term associated when converting value type to object type and Unboxing is just reverse that is converting object type into value type.

  • Boxing creates new object in managed heap memory and copies the data and Unboxing does the reverse.

  • Boxing is implicit whereas unboxing is explicit.

  • Example is as in the following where boxing is taking place when converting int to object and unboxing is happening when converting object back to:
    1. int a = 5;  
    2. object obj = a; //Boxing   
    3. a = (int) obj; //Unboxing  

Question 2: How constants and read-only variables are different? Explain with an example?

Answer:

  • Constants are implicitly static whereas Read only variables can be either instance type or static. Explicit static declaration on constant will throw an error.

  • Constants needs to declare their value in design time. Read only variables are computed at run-time.

  • Constants can be declared either at class level or inside a method.Read only variables can be either declared at class level or in the constructor at run time but they can’t be declared inside method.

  • Constant are used only with value types and string literals.Read only variables can hold both value type and reference type.

  • Constant should be used if its value is not going to change.Read only variables should be only if there is possibility of changes in value in runtime.

  • For any change in the constant value later, both current and referred assembly needs to be compiled whereas for any change in the Read only value later, only current assembly needs to be compiled and referred assembly will automatically be updated.

  • For example:
    1. ////Constant  
    2. public const string EmployeeTableNamev //Error  
    3. public static const string EmployeeTableNamev = "Employee"//Error  
    4. public const string EmployeeTableNamev = "Employee"//Correct  
    5.   
    6.   
    7. ////Read only  
    8. public readonly string EmployeeTableNamev; //Correct  
    9. public MyClass()  
    10. {  
    11.    EmployeeTableNamev = "Employee";  
    12. }  
    13.   
    14. public readonly string EmployeeTableNamev = "Employee"//Correct  
    15. public static readonly stringEmployeeTableNamev = "Employee"//Correct  

Question 3: What’s the similarity and difference among var, object and dynamic type?

Answer:

  • Var which introduced in .NET 3.0,can store all kind of data but initialization is required while declaration. Object is the base class of all .NET types and hence can also store all kind of data. Dynamicwhich introduced with .NET 4.0 internally uses object to store all kind of data.

  • Var enforces type safety by implicitly converting to its original type in design time itself whereas in case of object and dynamic, type conversion happens at runtime with the cost of boxing/unboxing operations.

  • Var cannot be used in passing method parameters and as a return type whereas object and dynamic can be used.

  • Var and dynamic type don’t require explicit casting when converting back to original type whereas object does.

  • Nowadays var is frequently used as it’s strongly typed while being readable. Object should be used when we have very little information about the data type. Dynamic should be used only when working with reflection or COM APIs.

  • Example is as following:
    1. var a; //Error  
    2. var b = 3; //Correct, internally it converts to int b = 3;  
    3. int c = b;//Correct, implicitly conversion   
    4.   
    5. object a; //Correct  
    6. object b = 7;//Correct  
    7. int c = b;//Error, cannot implicitly convert type object to int  
    8. int d = (int) b; //Correct  
    9.   
    10. dynamic a;//Correct  
    11. dynamic b = 7;//Correct  
    12. int c = b;//Correct  

Question 4: What’s the difference between string and string builder?

Answer:

Please refer my following article for the same.

Question 5: What are the types that can be created directly inside namespace?

There are some types as in the following which can be created directly inside namespace. Sometimes they are also called as first calls citizens:

  • Class - Primary building block of any program that is used to encapsulate variables, properties and methods into a single unit; reference type.
  • Interface - Keeps the signatures of methods, properties; reference type.
  • Struct - Somewhat similar to the class but a lightweight and value type.
  • Delegate - Used to encapsulate a named or anonymous method; reference type.
  • Enum - Set of named integer constants; value type.

Hope you have liked the article. Look forward for your comments/suggestions.

Up Next
    Ebook Download
    View all
    Learn
    View all