Skip to content

Instantly share code, notes, and snippets.

@josephwambura
Last active June 18, 2024 07:30
Show Gist options
  • Save josephwambura/003f364c45e7963ec753dd92f05ef569 to your computer and use it in GitHub Desktop.
Save josephwambura/003f364c45e7963ec753dd92f05ef569 to your computer and use it in GitHub Desktop.
C# interview questions and answers

10 advanced C# interview questions and answers for 2022

Do you wish to become a successful senior C# developer? Or, do you want to get the best C# senior developer on your team? My carefully curated list of advanced C# interview questions and answers should serve you as a very reliable guide.

C# interview questions and answers

  1. How do you do Exception Handling in C#?
    View answer
    The following four keywords are used for Exception Handling in C#:
    • Try - The try block recognizes which block of code has particular exceptions activated.
    • Catch - The catch keyword signifies a program for catching an exception using an exception handler.
    • Finally - The finally block executes a given block of code whether or not an exception is caught.
    • Throw - Using the throw keyword, the program throws an exception in the event of a problem.
  2. Explain Boxing and Unboxing.
    View answer
    Both Boxing and Unboxing are used for converting the type. However, there are some differences.
    • Boxing - Boxing converts the value type to the object or to the data type of an interface implemented by this particular value type. The CLR boxes a value, in other words, converts the value type to object. For this, CLR wraps the value in System.Object and stores it in the heap area within the domain of the application.
    • Unboxing - Unboxing extracts the value type from the object or any interface type that has been implemented. For Boxing, implicit code may be used, but for Unboxing explicit code must be used.

    Boxing and Unboxing highlight that C# has a unified view of the type system, meaning that all value types can be treated as objects.

  3. Differentiate between Managed and Unmanaged Code.
    View answer
    The difference between Managed and Unmanaged Code is as follows:
    • Managed Code - Managed Code is developed within the .NET framework. CLR directly executes such code by using managed code execution. Any language written in the .NET framework is considered to be managed code.
    • Unmanaged Code - Unmanaged code is any code developed outside the .NET framework. Unmanaged applications are not executed by CLR. Some languages like C++ can write unmanaged applications such as an application for accessing the low-level functions of the operating system. Some examples of unmanaged code include background compatibility with the code of VB, ASP, and COM.
  4. Differentiate between Struct and Class in C#.
    View answer
    Class and struct both are user-defined data types. However, they have some important differences:

    Struct

    • Struct is a value type in C# that inherits values from System.Value
    • It is mostly used for small quantities of data
    • It cannot be inherited to any other type
    • A Struct cannot have abstract values.

    Class

    • Class is a reference type in C#. Since it refers to objects, it inherits from System.Object
    • Classes are mostly used for large quantities of data
    • Classes can be inherited to other classes
    • Classes can have abstract values.
    • A default constructor can be created for classes.
  5. What is the difference between Task and Thread in C#?
    View answer
    Following are the differences between Task and Thread in C#:
    • Thread is an actual Operating System-level thread that has its own stack and kernel resources. Thread gives maximum control. In Thread, one can Abort (), Suspend (), or Resume (). One can observe the state of a Thread or set properties for it such as stack size, apartment state, and culture. The CLR maintains a pool of threads that are wrapped by ThreadPool.
    • The Task is a class in the Task Parallel Library. The Task doesn’t have its own Operating System thread, unlike the ThreadPool. The TaskScheduler executes tasks, but the default scheduler is run on the ThreadPool. It is possible to know when a Task finishes and get a result.
  6. How is encapsulation done in C#?
    View answer
    Access specifiers help implement Encapsulation, in C#, is implemented by using access specifiers. A class member’s scope and visibility are defined by these access specifiers.
    • With public access specifiers, a class can expose its member variables and functions to other objects and functions. Once a member is public, it can be reached from outside the class.
    • With private access specifiers, a class can hide its member variables and functions from other objects and functions. The private members of a class can be accessed only by functions of the same class. Even instances of the same class do not have access to its private members.
    • Protected access specifiers are similar to private access specifiers because they cannot be accessed outside the class. However, protected class members can be accessed by any subclass of that class as well. This enables implementing inheritance.
  7. What is a Destructor in C# and when is it used?
    View answer
    A Destructor helps in cleaning up the memory and freeing the resources. However, it is not needed in C# as such because the garbage collector does that in C#. System.GC.Collect () is internally called for the purpose. The need for a destructor may arise when a class holds certain expensive unmanaged resources that require cleaning after the object is removed. The disposable pattern is better when ensuring clean up of the resource even when the object-user forgets to dispose of it. Before creating a destructor, one must ensure that one has a clear understanding of the garbage collector’s function. This is especially because:
    • Destructors don't run on the user’s thread. They run on their own thread. Thus, one must ensure deadlocks are prevented.
    • When a destructor throws an unhandled exception, it is difficult to catch because it runs on its own thread.
    • One can call a destructor on an object in between the start and the finish of a constructor. If a destructor is written properly, it will not depend on the invariants defined in the constructor.
    • With the help of a destructor, an object can be resurrected, meaning a dead object can be brought to life.
    • It is not sure when the object will be scheduled for finalization and hence, the need for running a destructor may never arise.
  8. For methods inside the interface, why can you not specify the accessibility modifier?
    View answer
    Virtual methods in an interface have no method definition. The methods here are written to be overridden in the derived class and hence, they are publicly available.
  9. Differentiate between ref and out keywords.
    View answer
    Here’s the difference between ref and out keywords:
    • The ref keyword informs the compiler about object initialization before entering the function.
    • The out keyword informs the compiler about object initialization within the function.
  10. Why is finally block used in C#?
    View answer
    The finally block always gets executed if there is an exception or not. When the code is executed in the try block and an exception occurs, control returns to the catch block, and in the end, the finally block gets executed. The finally block therefore can contain closing connections to the database and release of file handlers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment