C sharp must read interview Questions and Answers

On August 28

C sharp must read interview Questions and Answers

C# Interview must read interview Questions and Answers


Is C# code is managed or unmanaged code?

C# is managed code as Common language runtime can compile C# code to Intermediate language.


What is the difference between directcast and ctype?

DirectCast is used to convert the type of an object that requires the run-time type to be the same as the specified type in DirectCast.

How to implement singleton design pattern in C#?

In singleton pattern, a class can only have one instance and provides access point to it globally.

  1. public sealed class Singleton  
  2. {  
  3.     private static readonly Singleton instance = new Singleton();  
  4.     // Explicit static constructor to tell C# compiler  
  5.     // not to mark type as beforefieldinit  
  6.     static Singleton()  
  7.     {  
  8.     }  
  9.     private Singleton()  
  10.     {  
  11.     }  
  12.     public static Singleton Instance  
  13.     {  
  14.         get  
  15.         {  
  16.             return instance;  
  17.         }  
  18.     }  
  19. }  

What is difference between the "throw" and "throw ex" in .NET?

"Throw" statement preserves original error stack whereas "throw ex" have the stack trace from their throw point. It is always advised to use "throw" because it provides more accurate error information.

 What is difference between is and as operators in c#?

  • The is operator is used to check if the run-time type of an object is compatible with the given type or not whereas as operator is used to perform conversion between compatible reference types or Nullable types.
  • The is operator is of boolean type whereas as operator is not of boolean type.
  • The is operator returns true if the given object is of the same type whereas asoperator returns the object when they are compatible with the given type.
  • The is operator returns false if the given object is not of the same type whereas asoperator return null if the conversion is not possible.
  • The is operator is used for only reference, boxing, and unboxing conversions whereas as operator is used only for nullable, reference and boxing conversions

 How to use nullable types in .Net?

Value types can take either their normal values or a null value. Such types are called nullable types.


What is the use of the “using” statement in C#?

There are two ways to use the using keyword in C#. One is as a directive and the other is as a statement. Let's explain!

  1. using Directive
    Generally we use the using keyword to add namespaces in code-behind and class files. Then it makes available all the classes, interfaces and abstract classes and their methods and properties in the current page. Adding a namespace can be done in the following two ways:
  1. Using Statement
    This is another way to use the using keyword in C#. It plays a vital role in improving performance in Garbage Collection.



You may also like :