C# Programming Interview Questions Tutorial
C# basics Interview Questions
Q. What is class?
A. Class is the blueprint of an object. Class is used to group the different related functionality.
Q. What are the value types and reference types?
A. Value types are those variables who are stored in stack. Following are the value types:
int, float, decimal, enum, byte, double, long, struct
Reference types are those stored in the heap.
object, class, interface, string
Q. What is the difference between ref & out parameters?
A. For both types of parameters, you have to declare the variable first. But in case of ref, that variable must be initialized, whereas in case of out, it needs not to be initialized.
Q. Can we use 'this' keyword in a static method?
A. We can't use this keyword inside a static method, because only static methods and variables can be used inside a static method.
Q. What is the difference between readonly and const keyword?
A. Const keyword is used to declear a constant variable, which will be burned into the compiled code. So to reset it, we have to recompile the code. While readonly is also used to make a constant at run time. It's value is assigned during declearation or in constructor.
Q. What is the difference between Array and Arraylist?
A. Array can have elements of same type. It has a fixed length. But in arrayist, length is not fixed.
Q. What is the difference between String and StringBuilder?
A. String is immutable. Everytime we update the value of string, new memory is allocated and previous one is released. StringBuilder is mutable. It gives us many operations options to change the string value without allocating the new memory.
Q. What is difference between class and struct?
A. Class and Struct are very similar. One difference between them is that class is object type while Struct is value type. So, Struct can be used for smaller size objects.
Struct doesn't support inheritance. So, it can't inherit any class or struct or any class or struct can't inherit struct. However, struct can implement interface as classes do. Some people call this interface inheritance.
Q. Can we declare more than one method with the same parameters and different return type?
A. No, we can not declare two methods with the same name and same parameters with the different return type. It will give a compile time error.
Q. What is boxing?
A. Boxing is the process of converting value type to corresponding reference type. Opposite of this is called un-boxing.
Q. Can be there a try block without catch and finally?
A. Yes, there can be try block without catch and finally.
Q. What is the difference between Finalize() and Dispose() methods?
A. Dispose() is called when we want for an object to release any unmanaged resources with them. If you want to use dispose on any class, then you must inherit that class from IDisposable interface. Dispose is called manually by user code.
On the other hand Finalize() is used for the same purpose but it doesn't assure the garbage collection of an object. It's called by garbage collector and can not be called manually by user code.
Q. What is unmanaged resources?
A. Unmanaged resources or objects are those which created outside of CRL and not controled by CLR and everything that garbage collector is unaware of, example of these unmanaged resources are file streams, COM objects, connection objects, open network connections.
Q. What is the purpose of the 'sealed' keyword?
A. The sealed keyword is used to make a class sealed, so no other class can inherit it.
Q. Explain the usage of the 'static' keyword.
A. The static keyword is used to declare static members and methods. if it's applied on a class, then all fields and methods of that class must be static. These are accessible globally.
Q. What is the difference between “continue” and “break” statements in C#?
A. Using break will take you out of the loop, while using continue control will get out of the current iteration and go to the next iteration.
Q. What is the difference between throw and throw exception?
A. "Throw" statement preserves original error stack whereas "throw ex" has the stack trace from their throw point. It is always advised to use "throw" because it provides more accurate error information.
Q. What are indexers in C# .NET?
A. Indexers are known as smart arrays in C#. It allows the instances of a class to be indexed in the same way as array.
// Indexer declaration
public int this[int index]
Q. What is a delegate in C#?
A. Delegates are pointers to functions. Delegates have only signature declaration, which matches the signature of the function it delegates to.
Think of delegates as method as parameter, like if you need to pass method as parameter to another method. 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 a multicast delegate?
A. A multicast delegate is a delegate with multiple handlers. So it means that delegates are assigned to multiple functions who have the same signature.
class CarProcess {
// Delegate
public delegate void CarAccessoriesHandler(Car car);
}
class Program{
static void Main(string[] args){
Program obj = new Program();
CarProcess.CarAccessoriesHandler carProcessHandler = obj.AddMusicSystem;
// Multicast delegate.
carProcessHandler += obj.AddAlloyWheels;
// Do other logic.
}
void AddMusicSystem(Car car){
Console.WriteLine("Added Music System");
}
void AddAlloyWheels(Car car){
Console.WriteLine("Added Alloy Wheels");
}
}
Q. What are partial classes?
A. Partial classes are the classes that are defined in two or more separate physical files. At compile time, they unite and create a single class as any normal class. But they must be in the same namespace.
Q. Why we use extension methods?
A. We use extension method when we don't have access of the class and we can't make changes in the class directly. These classes can be from .Net framework or form any third party library.