Top C# Interview question and answers

In this article, I will try to cover c# interview questions which you must know before going for an interview, this is useful for both freshers and experienced candidates.

1: Decimal vs Float vs Double

a) Use decimals for counted values like money. Use float/double for measured values like distance.

b) Double and Float can be divided by zero without any exception, while decimals cannot be divided by 0, the compilation will always fail.

c) Decimal has much higher precision and is usually used within the financial application that required a high degree of accuracy.

Precision:
Float 32 bit 7 digits
Double 64 bit 16 digits
Decimal 128 29 digit

d) A critical loss of precision is acceptable for specific calculation but not for financial applications.

e) The operation of the decimal is slower than float and double.

2 What is the difference between struct and class?

Struct:
1. The struct is a value type.
2. A struct is usually used for a small amount of data.
3. A struct can’t be abstract.
4. A struct can’t be inherited.
5. We don’t have permission to create a default constructor.
6. A struct is stored on the stack.

Class:
1. The class is a reference type in C#
2. Class is used for a large amount of data.
3. A class can be inherited.
4. A class can be abstract
5. We can create a default constructor
6. Classes are stored on the heap.

3. Difference between Value type and Reference type

Value types are stored generally in the stack, Reference types are stored in the managed heap. The stack is the example of the value type, while class is an example of reference type.

interview
stack vs heap

4 Value type and reference type performance?

a) Every time when you cast an int to an object a little piece of memory (on the heap) is reserved and the value of int is copied. Every time when we cast it back than .Net will first check if the object contains int, this extra overload will cause performance degradation on Heap.

b) Variable allocates on Stack are stored directly to the memory when the program is compiled. Variable allocates on Heap have their memory at runtime and accessing this memory is a bit slow.

5: What is the difference between Constant and Readonly?

Constants and Readonly both are immutable values, You can assay their values by constructor when we call the constructor with a new keyword.

6. When to use struct instead of class?

a) If we want good performance and data sets are small.
b) Structs are immutable.
c) Struct will not have to be boxed frequently.

7. Equals vs == Operator

Equals give value equality whereas == gives reference equality. For value type both works as expected.

8. Is vs As Operator

Is Operator is to check if the two objects are of the same type or not.
As Operator is useful when you want to typecase from one type to the other type.

9: What is the difference between Ref and Out?

Ref:
1. The parameter must be initiated first before it passed as ref.
2. Passing a parameter value of ref is useful when the called method is also needed to modify the passed parameter

Out:
1. The calling method must have to initialize the out keyword before returning.
2. The parameter value must be initialized with the calling method before use.

10: What are the different access specifiers in c#?

public: There is no restriction on accessing public specifier.
private: Limited within the class, private is the default access specifier.
protected: Limited to class and those inherits that.
internal: Access is limited to the assembly.
protected internal: Limited to class within the assembly or that inherits within the assembly.
private protected: A private protected member of a base class is accessible from derived types in its containing assembly only if the static type of the variable is the derived class type. (is valid in C# version 7.2 and later )

11: What is the difference between Finalize and Dispose?

1. Finalize is used to free the unmanaged code/resource like database files. Finalize cannot be called manually by the user whereas we can call dispose. Garbage collection calls the finalize.

2. We can use finalize when we have unmanaged resources in our code. We can use dispose while creating a custom class that will be used by the other users.

12: What is the difference between IEnumerable and IEnumerator?

IEnumerable is the parent interface of all collections(non-generic). Both IEnumerable and IEnumerator helps to loop through the collection. IEnumerable actually uses IEnumerator to remember the state i.e value and state.

13: What are Generics?

Generics make your type independent of type. We can use Generics to avoid boxing and unboxing.

public static bool AreEqual<T>(T value1, T value2)
{
return value1.Equals(value2);
}

14: What is Collection?

A collection is a group of records that can be treated as a logical name. There are different types of collection in c#.

1. Index-based
a) Array
b) List

2. Key value-based
a) Hashtable
b) Sorted List

3. Prioritized based
a) Stack
b) Queue

4. Specialized Collections
a) String
b) Dictionary

15: What is a Delegate?

Delegate is a type-safe function pointer. If we invoke the delegate the function will get invoked.

1. Delegates can be used to inject the logic inside a function.
2. With the help of delegates, decoupling can be achieved.
3. Lambda expression is based on delegates.

Action vs Func

Action: Delegate with void return type with no parameters.
Action<T>, Action<T1, Action T2>

Func: Delegate with custom return type with no parameters.
Example:
func<int, string, bool> myDelegate;
delegate bool MyDelegate(int a, string b);

16: What is a multicast delegate?

A multicast delegate is a delegate that can hold more than 1 delegates. We can use += to assign multiple delegates.

Example: delegate4 = delegate1+ delegate2 + delegate3;

** Multicast delegates make the implementation of observer design pattern very simple.

17: What is the difference between Events and Delegates?

A class can subscribe or unsubscribe the events unlike we can do the assignments if delegates. The assignment of delegates may cause an update.

Stay tuned and keep checking for all the c# interview questions and answers. Good luck for the interview!

For more articles related to C#. Please visit this.
Related Post: OOPS interview questions and answers

1 thought on “Top C# Interview question and answers”

Leave a Comment

Your email address will not be published.