How do you do Exception Handling in C#?
The following four keywords are used for Exception Handling in C#:
Explain Boxing and Unboxing.
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.
Differentiate between Managed and Unmanaged Code.
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.
Differentiate between Struct and Class in C#.
Class and struct both are user-defined data types. However, they have some important differences:
What is the difference between Task and Thread in C#?
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.
How is encapsulation done in C#?
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.
What is a Destructor in C# and when is it used?
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:
For methods inside the interface, why can you not specify the accessibility modifier?
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.
Differentiate between ref and out keywords.
Here’s the difference between ref and out keywords:
Why is finally block used in C#?
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.
We hope that the above list of C# interview questions helps you regardless of whether you are a company looking for a C# developer or a C# developer looking for a senior position. Apart from the technical C# interview questions, it will be good to prepare answers to soft skills questions as well. Recruiters wishing to hire experienced C# developers must ensure they hire people who not only ace the C# interview questions but also gel with their company culture.
If you are a company looking to enhance the capacity of your C# developer team, Turing can help you hire from among the top 1% C# developers. If you are a senior C# developer looking for a senior position with a Silicon Valley company, apply to C# developer jobs at Turing today.
Tell us the skills you need and we'll find the best developer for you in days, not weeks.