C# Interview Questions and Answers

To help you prepare for your interview, this article briefly includes a set of top C# interview questions and answers covering almost all of its important topics in simple terms.

Questions & Answers

 

  1. What is C-Sharp (C#)?

    • Answer: C# is a type-safe, managed and object oriented language, which is compiled by .Net framework for generating intermediate language (IL).
  2. What is an Object and a Class?

    • Answer: Class is an encapsulation of properties and methods used to represent an entity in real time. It is structure of data that brings together all instances in one unit.
    • An object is class instance. Technically, it is only block of allocated memory that can be stored as Variables, Array or Collection.
  3. What are the fundamental OOP(Object Oriented Programming) concepts?

    • Answer: The four fundamental concepts of Object Oriented Programming are:
      1. Encapsulation: Encapsulation means hiding attributes in programming so that you can make changes in one place without having to also make changes in the other parts of an application.
      2. Abstraction: It is process in which an object’s critical behavior and data are identified and irrelevant details eliminated.
      3. Inheritance: Inheritance refers to the use of super class’s structure and behavior in subclass. 
      4. Polymorphism: Polymorphism refers to changing super – class behavior in the subclass.
  4. Which class is super class of all classes in .NET Classes?

    • Answer: Object class (System. Object) is the. NET class base.
  5. Why Main() method is static ?

    • Answer: Your program needs an entry point. Without instantiating an object, static method can be called. Hence main() must be static to allow it to be your program’s entry.
  6. What is Managed and Unmanaged code?

    • Answer:
      • Managed: code written in .NET language like C#, VB.NET.
      • Unmanaged: code not written in .NET language and MSIL does not understand what it is and can not run under CLR; like third-party controls we used in our .NET applications which is not created in .NET languages.
  7. What is an Interface?

    • AnswerInterfaces are powerful programming tool because they allow you to separate object definition from implementation.
  8. What are the differences between a Class and a Struct?

    • Answer: Class and struct are both the type of data defined by the user but have some significant differences:
      • Struct
        1. The struct is value type and it inherits from System.Value Type.
        2. Struct is usually used for smaller amounts of data.
        3. Struct can’t be inherited to other types.
        4. A structure cannot be abstract.
        5. No need to create object by new keyword.
        6. Do not have permission to create any default constructor.
      • Class
        1. The class is reference type and it inherits from the System.Object Type.
        2. Classes are usually used for large amounts of data.
        3. Classes can be inherited to other class.
        4. A class can be abstract type.
        5. We can create a default constructor.
  9. What is the difference between Virtual method and Abstract method?

    • Answer: There can be no functionality for an abstract function. You’re basically saying that any child class MUST give its own version of this method, but even trying to implement it in the parent class is too general.
    • virtual function, basically says look, here’s the functionality that may or may not be good enough for the class of the child. So if it’s good enough, use this method, if not, override me and provide your own features.
  10. How to implement Exception Handling in C#?

    • Answer: Four keywords for Exception handling  are used in C#
      1. try: Contains block of code that will be checked for an exception.
      2. catchIt is program that, with the help of an exception handler, catches an exception.
      3. finallyIt is code block written to execute irrespective of whether or not an exception is caught.
      4. throw: Throws an exception when a problem occurs.
  11. What is a Jagged Array?

    • Answer: Jagged array is an array that contains arrays of elements. It’s called the array of arrays as well. It can be single or multi – dimensional.
      • Example: int[] jaggedArray = new int[10][];
  12. Can we have static methods in interface?

    • Answer: On an interface in C#, we can’t define static members. An interface is contract, not an implementation.
  13. What is Constructor?

    • Ansewer: In class that has the same name as its class, constructor is member function. When an object class is created, the constructor is automatically invoked. It builds data members ‘ values while initializing the class.
  14. What is Serialization?

    • Answer: Serialization is process in which code is converted to its binary format. It can be easily stored and written to disk or any such storage device once it is converted to bytes.
  15. What is the difference between “==” and “Equals()” in C#?

    • AnswerThe “= =” method compares object references but Equals method compares object content.