C# Programming Interview Questions Tutorial
C# interview questions for 3 - 5 years experienced
Q. What is thread safety in C#?
A. Thread safety means, in a multithreaded application, a shared object is read or write by only one thread at a specific time. For that, we use a lock keyword on the shared object.
Q. What is thread affinity in C#?
A. Thread affinity means that in a multithreaded application, an object is accessed only by that thread which created it. So other threads will not be able to access that object. In case it's needed to access that object by other threads, which not created that object, then marshaling a request to creator thread is required.
Q. async/await in C# uses single thread or multiple thread?
A. Async/Await uses single thread. It doesn't create a new thread. It starts process in a non blocking way, so meanwhile thread is waiting for any task to complete, it can work on any other task if asked for.
Q. Is string reference type or value type?
A. String is a reference type but behaves like value type, for example, you can observe the value type behavior for string like being immutable and we can use == operator to compare values as we commonly do with value type rather than comparing the object.
The reason why string is that way is because strings can be huge that's why they need to be saved on Heap, but to give developer ease to use string value type behavior are provided.
Q. What is the difference between string, StringBuilder and StringBuffer in C#?
A. string is immutable, means string value can not be changed.
StringBuilder and StringBuffer both are mutable. The difference between these two is that StringBuffer is synchronized, which make it thread-safe and because of that, it's slower. On the other hand, StringBuilder is not synchronized, so it's not thread-safe, and this makes it faster.
Following are the details:
Parameters | string | StringBuilder | StringBuffer |
Mutable | No | Yes | Yes |
Performance | Slow | Very Fast | Fast |
Storage | Constant String pool | Heap | Heap |
Synchronization | Yes | No | Yes |
Thread-safe | Yes | No | Yes |
Q. What is anonymous method in C#?
A. As the name suggests, anonymous method is a method without any name.
Q. What are delegates?
A. Delegates are helpful in situations, where we need to pass method instead of data variable in another method parameter. Mostly useful in situations where we don't know exactly what method we need to pass and it is known only at runtime.
Q. What is lambda expression?
A. Lymbda expression is syntx of assigning code implemenation to delegates. They can be used whenever there s a delegate parameter type. So they are kind of fancy replacement of anonymous methods, and make code more readable and beautiful.
Q. What is the difference between compile time and run time polymorphism?
A. Compile time and run time polymorphism is just another name for overloding and overriding. Here is some other names or terms used:
1- Overloading - compile time polymorphism or early binding
2- Overriding - run time polymorphism or late binding.
Q. What is the difference between var and dynamic?
A. var is used to declare variable without specifying the .Net type explicitly and .net type is assigned implicitly based on the type of value assigned to this variable. It was introduced in C# 3. Dynamic keyword is used to declare a variable when we want to avoid compile time type check, instead .net type is known only on runtime. This feature was introduced in C# 4.0.
var keyword can be useful in some special-case situation like in Linq.
Q. What is the usage of GC.SupressFinalize?
A. GC.SupressFinalize is used to prevent to execute the Finalizer of the specified object. The object, whose finalizer must not excute, is passed as parameter. Eg: GC.SupressFinalize(myObjectname);
Q. Is empty destructors good or bad?
A. Adding empty destructor is bad, because what it does is that it adds an entry for that class in the Finalize queue and then GC has to process it, which is completely unnecessary because destructor has nothing. Any unnecessary work for GC cause performance issues.
Whenever we add destructor to a class, it generates Finalizer for that class and it is added to GC Finalize queue.
Read Tips for C# performance issues
Q. What is silent failure?
A. Silent failure is when any arithmetic overflow exception is happened but it is not raised. It generally happens when a numeric data type (e.g. int) is assigned a value large than it capacity at runtime. To catch these exceptions, we can use "checked" keyword.
Q. What is checked keyword?
A. Checked keyword is used to check for any arithmetic overflow in the program. This will help us to detect the arithmetic overflow exceptions, although it makes program slower. There is also option available to set checked enable for whole program in the project setting.
checked {
try {
int largeNumber = 5000000000;
int sum = largeNumber + largeNumber;
Console.WriteLine("Sum is: " + sum);
}
catch (Exception ex) {
Console.WriteLine("Exception Happened");
}
}
Q. What is unchecked keyword?
A. Unchecked keyword is used for skipping arithmetic overflow in the program when arithmetic overflow check if enabled on the project level.