Preparing for .NET Interviews - Part Four (Operators)

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

So let’s takethe questions one by one.

1. What’s sizeof operator and how to use it?

Answer:

sizeof operator is used to return the size of any data type allocated by CLR in bytes.

For example,

  1. Console.Title = "C# Operator demo";  
  2.   
  3. var size = sizeof(int);  
  4.   
  5. Console.WriteLine("Size of type in bytes is: {0}", size);  
Output: 

Please note that, to obtain size of built-in types such as int, char, float, bool, decimal, sizeof can directly be used however for other types such as structs, enum and pointer types it can be used only in unsafe code blocks as following.

  1. struct EmployeeStruct  
  2.   
  3. {  
  4.   
  5.     int empId;  
  6.   
  7.     int salary;  
  8.   
  9. }  
  10.   
  11. public class Program  
  12.   
  13. {  
  14.   
  15.     static void Main()  
  16.   
  17.     {  
  18.   
  19.         Console.Title = "C# Operator demo";  
  20.   
  21.         int size = 0;  
  22.   
  23.         unsafe  
  24.   
  25.         {  
  26.   
  27.             size = sizeof(EmployeeStruct);  
  28.   
  29.         }  
  30.   
  31.         Console.WriteLine("Size of type in bytes is: {0}", size);  
  32.   
  33.     }  
  34.   
  35. }   

Output:

 

Just to add that to enable unsafe compilation, you need to check the “Allow unsafe code” check box in project properties -> Build tab. Also please don’t get confused with Marshal.SizeOf Method because it returns the size after marshaling and that may not be the same as allocated by CLR.

2. What’s the similarity and difference between typeof and GetType?

Answer:

The similarity between typeof and GetType is that both return System.Type instance. However the difference is in their uses.

typeof is used to obtain the type at compile time and the operand of typeof is always a type directly whereas GetType is used to know the type of information at run time and it can be used individually at the objects.

GetType can also be used with expressions as it calculates the type at runtime. Example is as follows.

  1. class MyClass  
  2. {  
  3.     public string Name  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8. }  
  9. public class Program  
  10. {  
  11.     static void Main()  
  12.     {  
  13.         Console.Title = "C# Operator demo";  
  14.         Console.WriteLine("Type is: {0}"typeof(int));  
  15.         Console.WriteLine("Type is: {0}"typeof(float));  
  16.         Console.WriteLine("Type is: {0}"typeof(bool));  
  17.         Console.WriteLine("Type is: {0}"typeof(char));  
  18.         MyClass obj = new MyClass();  
  19.         Console.WriteLine("Type is: {0}", obj.GetType());  
  20.     }  
  21. }  

Output:



3.
Difference between is and as operator in C#.

Answer:

You may refer to my blog as follows for the same.

I hope you liked the article. I look forward to your comments/suggestions.