C# .NET interview questions

C# .NET quiz questions

  • 1.

    Explain Anonymous type in C#?

    Answer:

    This is being added in C# 3.0 version. This feature enables us to create an object at compile time. Below is the sample code for the same –

    Var myTestCategory = new { CategoryId = 1, CategoryName = “Category1”};
    View
  • 2.

    Explain Partial Class in C#?

    Answer:

    Partial classes concept added in .Net Framework 2.0 and it allows us to split the business logic in multiple files with the same class name along with “partial” keyword.

    View
  • 3.

    What you mean by boxing and unboxing in C#?

    Answer:

    Boxing – This is the process of converting from value type to reference type. For example,

    int myvar = 10;
    object myObj = myvar;

    UnBoxing – It’s completely opposite to boxing. It’s the process of converting reference type to value type. For example,

    int myvar2 = (int)myObj;
    View
  • 4.

    Write a sample code to write the contents to text file in C#?

    Answer:

    Below is the sample code to write the contents to text file –

    Using System.IO;
    File.WriteAllText(”mytextfilePath”, “MyTestContent”);
    View
  • 5.

    What is the difference between “continue” and “break” statements in C#?

    Answer:

    • “continue” statement is used to pass the control to next iteration. This statement can be used with – “while”, “for”, “foreach” loops.
    • “break” statement is used to exit the loop.
    View
  • 6.

    Which are the loop types available in C#?

    Answer:

    Below are the loop types in C# -

    For
    While
    Do.. While

    View
  • 7.

    What is enum in C#?

    Answer:

    enum keyword is used for declaring an enumeration, which consists of named constants and it is called as enumerator lists. Enums are value types in C# and these can’t be inherited. Below is the sample code of using Enums

    Eg: enum Fruits { Apple, Orange, Banana, WaterMelon};
    View
  • 8.

    How to check whether hash table contains specific key in C#?

    Answer:

    Method – “ContainsKey” can be used to check the key in hash table. Below is the sample code for the same –

    Eg: myHashtbl.ContainsKey("1");
    View
  • 9.

    Explain Hashtable in C#?

    Answer:

    It is used to store the key/value pairs based on hash code of the key. Key will be used to access the element in the collection. For example,

    Hashtable myHashtbl = new Hashtable();
    myHashtbl.Add("1", "TestValue1");
    myHashtbl.Add("2", "TestValue2");

     

    View
  • 10.

    Why to use lock statement in C#?

    Answer:

    Lock will make sure one thread will not intercept the other thread which is running the part of code. So lock statement will make the thread wait, block till the object is being released.

    View
  • 11.

     Is C# code is unmanaged or managed code?

    Answer:

    C# code is managed code because the compiler – CLR will compile the code to Intermediate Language.

    View
  • 12.

    What is the difference between CType and Directcast in C#?

    Answer:

    • CType is used for conversion between type and the expression.
    • Directcast is used for converting the object type which requires run time type to be the same as specified type.
    View
  • 13.

    Define Multicast Delegate in C#?

    Answer:

    A delegate with multiple handlers are called as multicast delegate. The example to demonstrate the same is given below

    public delegate void CalculateMyNumbers(int x, int y);
    int x = 6;
    int y = 7;
    CalculateMyNumbers addMyNumbers = new CalculateMyNumbers(FuncForAddingNumbers);
    CalculateMyNumbers multiplyMyNumbers = new CalculateMyNumbers(FuncForMultiplyingNumbers);
    CalculateMyNumbers multiCast = (CalculateMyNumbers)Delegate.Combine (addMyNumbers, multiplyMyNumbers);
    multiCast.Invoke(a,b);
    View
  • 14.

    What is the difference between “as” and “is” operators in C#?

    Answer:

    • “as” operator is used for casting object to type or class.
    • “is” operator is used for checking the object with type and this will return a Boolean value.
    View
  • 15.

    Why to use “Nullable Coalescing Operator” (??) in C#?

    Answer:

    Nullable Coalescing Operator can be used with reference types and nullable value types. So if the first operand of the expression is null then the value of second operand is assigned to the variable. For example,

    double? myFirstno = null;
    double mySecno;
    mySecno = myFirstno ?? 10.11;
    View
  • 16.

    What is Nullable Types in C#?

    Answer:

    Variable types does not hold null values so to hold the null values we have to use nullable types. So nullable types can have values either null or other values as well.

    Eg: Int? mynullablevar = null;
    View
  • 17.

    What are the uses of delegates in C#?

    Answer:

    Below are the list of uses of delegates in C# -

    • Callback Mechanism
    • Asynchronous Processing
    • Abstract and Encapsulate method
    • Multicasting

     

    View
  • 18.

    Can we use delegates for asynchronous method calls in C#?

    Answer:

    Yes. We can use delegates for asynchronous method calls.

    View
  • 19.

    What are the differences between events and delegates in C#?

    Answer:

    Main difference between event and delegate is event will provide one more of encapsulation over delegates. So when you are using events destination will listen to it but delegates are naked, which works in subscriber/destination model.

    View
  • 20.

    What are the three types of Generic delegates in C#?

    Answer:

    Below are the three types of generic delegates in C# -

    • Func
    • Action
    • Predicate
    View

© 2017 QuizBucket.org