C# Comprehensive Interview Questions Bank Interview Questions and Answers
Practice every question in this topic using expandable answers and interview tips. Answer aloud first, then open the explanation.
Back to the complete interview hub
Q1. What is the difference between a struct and a class in C#? [Easy]
Answer: A class is a reference type stored on the heap, supporting inheritance and polymorphism. A struct is a value type stored on the stack, offering better performance for small data structures, but does not support inheritance.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q2. What is a partial class? [Easy]
Answer: A partial class allows the definition of a single class to be split across multiple files. The compiler combines them into a single class during compilation. It is useful for separating auto-generated code from user-written code.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q3. What is a sealed class? [Easy]
Answer: A sealed class is a class that cannot be inherited by any other class. It is created using the 'sealed' keyword to restrict inheritance and can provide performance benefits due to devirtualization.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q4. What are the characteristics of a static class? [Easy]
Answer: A static class cannot be instantiated using the 'new' keyword, can only contain static members, is sealed by default, and remains in memory for the lifetime of the application domain.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q5. What is method hiding (shadowing) in C#? [Medium]
Answer: Method hiding occurs when a derived class defines a method with the same name as a base class method using the 'new' keyword. It hides the base class implementation instead of overriding it.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q6. What is the CLR? [Medium]
Answer: The Common Language Runtime (CLR) is the execution engine of .NET. It manages memory, handles exception handling, provides security, and converts Intermediate Language (IL) code into native machine code.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q7. What is a JIT compiler? [Medium]
Answer: The Just-In-Time (JIT) compiler is a part of the CLR that converts MSIL (Microsoft Intermediate Language) code into native machine code just before it gets executed by the CPU.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q8. What is the difference between CTS and CLS? [Medium]
Answer: CTS (Common Type System) defines all basic data types supported by .NET. CLS (Common Language Specification) is a subset of CTS that defines rules .NET languages must follow to ensure cross-language interoperability.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q9. What is the difference between Managed and Unmanaged code? [Easy]
Answer: Managed code runs under the control of the CLR, which handles memory and security. Unmanaged code runs directly on the OS outside the CLR (like C/C++ pointers or Win32 APIs) and requires manual memory management.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q10. What is the 'params' keyword? [Easy]
Answer: The 'params' keyword allows a method to accept a variable number of arguments of a specific type. It must be the last parameter in the method signature, and no additional parameters are permitted after it.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q11. What are Action, Func, and Predicate delegates? [Medium]
Answer: They are built-in generic delegates. 'Action' takes up to 16 parameters but returns void. 'Func' takes up to 16 parameters and must return a value. 'Predicate' takes one parameter and strictly returns a boolean.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q12. What is the difference between Dictionary and Hashtable? [Medium]
Answer: Dictionary<TKey, TValue> is generic, type-safe, and faster. Hashtable is non-generic (stores objects), requires boxing/unboxing, and is loosely typed. Both store key-value pairs.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q13. What is the difference between a Task and a Thread? [Hard]
Answer: A Thread represents an OS-level thread and is heavy to create. A Task is a higher-level abstraction managed by the ThreadPool, supports async/await, returning values, and exception handling natively.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q14. What is a Deadlock in C# and how do you prevent it? [Hard]
Answer: A deadlock occurs when two or more threads block each other indefinitely, waiting for locks held by one another. Prevent it by locking objects in a consistent order, using timeouts, or favoring asynchronous non-blocking code.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q15. What are Records in C#? [Medium]
Answer: Introduced in C# 9, records are reference types that provide built-in functionality for value-based equality. They are ideal for immutable data models and use the 'with' expression for non-destructive mutation.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q16. What is Pattern Matching in C#? [Medium]
Answer: Pattern matching allows you to test an expression against a specific shape or type and optionally extract data from it. It is heavily used in 'is' expressions and modern 'switch' statements.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q17. What is the difference between 'dynamic' and 'object'? [Medium]
Answer: Variables of type 'object' require explicit casting to access members, and type checking happens at compile time. 'dynamic' bypasses compile-time type checking, deferring it entirely to runtime.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q18. What is the difference between IEnumerable and IEnumerator? [Hard]
Answer: IEnumerable is an interface that exposes the GetEnumerator() method. IEnumerator is the underlying interface that actually iterates through the collection using MoveNext(), Current, and Reset().
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q19. What does the 'lock' keyword do? [Medium]
Answer: The 'lock' statement acquires a mutual-exclusion lock for a given object, executes a statement block, and then releases the lock, ensuring that only one thread can access a critical section of code at a time.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q20. What is a Tuple in C#? [Easy]
Answer: A Tuple is a lightweight data structure used to group multiple distinct values into a single object without needing to define a custom class or struct. Modern C# uses ValueTuple for better performance and named elements.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q21. What is an Indexer in C#? [Medium]
Answer: An indexer allows instances of a class or struct to be indexed just like arrays. They are defined using the 'this' keyword and get/set accessors.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q22. What are Nullable types? [Easy]
Answer: Value types cannot naturally hold a null value. Nullable types, declared using a question mark (e.g., int?), allow value types to represent a normal range of values plus a null value.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q23. What is the null-conditional operator (?.)? [Easy]
Answer: The null-conditional operator safely accesses members or elements only if the object is not null. If the object is null, the expression returns null instead of throwing a NullReferenceException.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q24. What is the null-coalescing operator (??)? [Easy]
Answer: The null-coalescing operator returns the left-hand operand if it is not null; otherwise, it evaluates and returns the right-hand operand.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q25. How do you implement a Thread-Safe Singleton pattern in C#? [Hard]
Answer: It is best implemented using the 'Lazy<T>' type or by using a static readonly field initialized inline, which the CLR guarantees to be thread-safe. Older methods involve double-checked locking.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q26. What is the difference between Finalize and Dispose? [Hard]
Answer: Dispose() is called explicitly by the developer (often via 'using') to free unmanaged resources immediately. Finalize() (Destructor) is called implicitly by the Garbage Collector before memory is reclaimed.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q27. What is an Assembly in .NET? [Medium]
Answer: An assembly is a compiled code library used by .NET for deployment, versioning, and security. It takes the form of an executable (.exe) or dynamic link library (.dll) file.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q28. What is Reflection in C#? [Hard]
Answer: Reflection allows a program to inspect its own metadata at runtime. Using the System.Reflection namespace, you can dynamically create objects, invoke methods, and read custom attributes.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q29. What are Attributes in C#? [Medium]
Answer: Attributes are tags applied to code elements (classes, methods, properties) to inject metadata. This metadata can be consumed at runtime using Reflection for behaviors like serialization or validation.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q30. What is the difference between a Shallow Copy and a Deep Copy? [Hard]
Answer: A shallow copy duplicates an object's value types but only copies references for reference types (both objects point to the same memory). A deep copy clones the object and all dynamically allocated memory it points to.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q31. What is a Mutex? [Medium]
Answer: A Mutex (Mutually Exclusive) is a synchronization primitive used to grant exclusive access to a shared resource across multiple threads or even multiple processes.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q32. What is covariance and contravariance in C#? [Hard]
Answer: Covariance (out keyword) allows you to use a more derived type than specified by the generic parameter. Contravariance (in keyword) allows you to use a less derived type. They ensure type safety in generic interfaces and delegates.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Study checklist
- Explain the answer without reading.
- Write one small example.
- Compare framework or database version differences in official documentation.
C# Comprehensive Interview Questions Bank Interview Questions and Answers
Practice one question at a time. Read the question, answer aloud, then compare your explanation with the answer.
← Back to the complete interview hub
Q1. What is the difference between a struct and a class in C#?
Easy
Answer: A class is a reference type stored on the heap, supporting inheritance and polymorphism. A struct is a value type stored on the stack, offering better performance for small data structures, but does not support inheritance.
Interview tip: Use a short example and connect the answer to real project work.
Q2. What is a partial class?
Easy
Answer: A partial class allows the definition of a single class to be split across multiple files. The compiler combines them into a single class during compilation. It is useful for separating auto-generated code from user-written code.
Interview tip: Use a short example and connect the answer to real project work.
Q3. What is a sealed class?
Easy
Answer: A sealed class is a class that cannot be inherited by any other class. It is created using the 'sealed' keyword to restrict inheritance and can provide performance benefits due to devirtualization.
Interview tip: Use a short example and connect the answer to real project work.
Q4. What are the characteristics of a static class?
Easy
Answer: A static class cannot be instantiated using the 'new' keyword, can only contain static members, is sealed by default, and remains in memory for the lifetime of the application domain.
Interview tip: Use a short example and connect the answer to real project work.
Q5. What is method hiding (shadowing) in C#?
Medium
Answer: Method hiding occurs when a derived class defines a method with the same name as a base class method using the 'new' keyword. It hides the base class implementation instead of overriding it.
Interview tip: Use a short example and connect the answer to real project work.
Q6. What is the CLR?
Medium
Answer: The Common Language Runtime (CLR) is the execution engine of .NET. It manages memory, handles exception handling, provides security, and converts Intermediate Language (IL) code into native machine code.
Interview tip: Use a short example and connect the answer to real project work.
Q7. What is a JIT compiler?
Medium
Answer: The Just-In-Time (JIT) compiler is a part of the CLR that converts MSIL (Microsoft Intermediate Language) code into native machine code just before it gets executed by the CPU.
Interview tip: Use a short example and connect the answer to real project work.
Q8. What is the difference between CTS and CLS?
Medium
Answer: CTS (Common Type System) defines all basic data types supported by .NET. CLS (Common Language Specification) is a subset of CTS that defines rules .NET languages must follow to ensure cross-language interoperability.
Interview tip: Use a short example and connect the answer to real project work.
Q9. What is the difference between Managed and Unmanaged code?
Easy
Answer: Managed code runs under the control of the CLR, which handles memory and security. Unmanaged code runs directly on the OS outside the CLR (like C/C++ pointers or Win32 APIs) and requires manual memory management.
Interview tip: Use a short example and connect the answer to real project work.
Q10. What is the 'params' keyword?
Easy
Answer: The 'params' keyword allows a method to accept a variable number of arguments of a specific type. It must be the last parameter in the method signature, and no additional parameters are permitted after it.
Interview tip: Use a short example and connect the answer to real project work.
Q11. What are Action, Func, and Predicate delegates?
Medium
Answer: They are built-in generic delegates. 'Action' takes up to 16 parameters but returns void. 'Func' takes up to 16 parameters and must return a value. 'Predicate' takes one parameter and strictly returns a boolean.
Interview tip: Use a short example and connect the answer to real project work.
Q12. What is the difference between Dictionary and Hashtable?
Medium
Answer: Dictionary<TKey, TValue> is generic, type-safe, and faster. Hashtable is non-generic (stores objects), requires boxing/unboxing, and is loosely typed. Both store key-value pairs.
Interview tip: Use a short example and connect the answer to real project work.
Q13. What is the difference between a Task and a Thread?
Hard
Answer: A Thread represents an OS-level thread and is heavy to create. A Task is a higher-level abstraction managed by the ThreadPool, supports async/await, returning values, and exception handling natively.
Interview tip: Use a short example and connect the answer to real project work.
Q14. What is a Deadlock in C# and how do you prevent it?
Hard
Answer: A deadlock occurs when two or more threads block each other indefinitely, waiting for locks held by one another. Prevent it by locking objects in a consistent order, using timeouts, or favoring asynchronous non-blocking code.
Interview tip: Use a short example and connect the answer to real project work.
Q15. What are Records in C#?
Medium
Answer: Introduced in C# 9, records are reference types that provide built-in functionality for value-based equality. They are ideal for immutable data models and use the 'with' expression for non-destructive mutation.
Interview tip: Use a short example and connect the answer to real project work.
Q16. What is Pattern Matching in C#?
Medium
Answer: Pattern matching allows you to test an expression against a specific shape or type and optionally extract data from it. It is heavily used in 'is' expressions and modern 'switch' statements.
Interview tip: Use a short example and connect the answer to real project work.
Q17. What is the difference between 'dynamic' and 'object'?
Medium
Answer: Variables of type 'object' require explicit casting to access members, and type checking happens at compile time. 'dynamic' bypasses compile-time type checking, deferring it entirely to runtime.
Interview tip: Use a short example and connect the answer to real project work.
Q18. What is the difference between IEnumerable and IEnumerator?
Hard
Answer: IEnumerable is an interface that exposes the GetEnumerator() method. IEnumerator is the underlying interface that actually iterates through the collection using MoveNext(), Current, and Reset().
Interview tip: Use a short example and connect the answer to real project work.
Q19. What does the 'lock' keyword do?
Medium
Answer: The 'lock' statement acquires a mutual-exclusion lock for a given object, executes a statement block, and then releases the lock, ensuring that only one thread can access a critical section of code at a time.
Interview tip: Use a short example and connect the answer to real project work.
Q20. What is a Tuple in C#?
Easy
Answer: A Tuple is a lightweight data structure used to group multiple distinct values into a single object without needing to define a custom class or struct. Modern C# uses ValueTuple for better performance and named elements.
Interview tip: Use a short example and connect the answer to real project work.
Q21. What is an Indexer in C#?
Medium
Answer: An indexer allows instances of a class or struct to be indexed just like arrays. They are defined using the 'this' keyword and get/set accessors.
Interview tip: Use a short example and connect the answer to real project work.
Q22. What are Nullable types?
Easy
Answer: Value types cannot naturally hold a null value. Nullable types, declared using a question mark (e.g., int?), allow value types to represent a normal range of values plus a null value.
Interview tip: Use a short example and connect the answer to real project work.
Q23. What is the null-conditional operator (?.)?
Easy
Answer: The null-conditional operator safely accesses members or elements only if the object is not null. If the object is null, the expression returns null instead of throwing a NullReferenceException.
Interview tip: Use a short example and connect the answer to real project work.
Q24. What is the null-coalescing operator (??)?
Easy
Answer: The null-coalescing operator returns the left-hand operand if it is not null; otherwise, it evaluates and returns the right-hand operand.
Interview tip: Use a short example and connect the answer to real project work.
Q25. How do you implement a Thread-Safe Singleton pattern in C#?
Hard
Answer: It is best implemented using the 'Lazy<T>' type or by using a static readonly field initialized inline, which the CLR guarantees to be thread-safe. Older methods involve double-checked locking.
Interview tip: Use a short example and connect the answer to real project work.
Q26. What is the difference between Finalize and Dispose?
Hard
Answer: Dispose() is called explicitly by the developer (often via 'using') to free unmanaged resources immediately. Finalize() (Destructor) is called implicitly by the Garbage Collector before memory is reclaimed.
Interview tip: Use a short example and connect the answer to real project work.
Q27. What is an Assembly in .NET?
Medium
Answer: An assembly is a compiled code library used by .NET for deployment, versioning, and security. It takes the form of an executable (.exe) or dynamic link library (.dll) file.
Interview tip: Use a short example and connect the answer to real project work.
Q28. What is Reflection in C#?
Hard
Answer: Reflection allows a program to inspect its own metadata at runtime. Using the System.Reflection namespace, you can dynamically create objects, invoke methods, and read custom attributes.
Interview tip: Use a short example and connect the answer to real project work.
Q29. What are Attributes in C#?
Medium
Answer: Attributes are tags applied to code elements (classes, methods, properties) to inject metadata. This metadata can be consumed at runtime using Reflection for behaviors like serialization or validation.
Interview tip: Use a short example and connect the answer to real project work.
Q30. What is the difference between a Shallow Copy and a Deep Copy?
Hard
Answer: A shallow copy duplicates an object's value types but only copies references for reference types (both objects point to the same memory). A deep copy clones the object and all dynamically allocated memory it points to.
Interview tip: Use a short example and connect the answer to real project work.
Q31. What is a Mutex?
Medium
Answer: A Mutex (Mutually Exclusive) is a synchronization primitive used to grant exclusive access to a shared resource across multiple threads or even multiple processes.
Interview tip: Use a short example and connect the answer to real project work.
Q32. What is covariance and contravariance in C#?
Hard
Answer: Covariance (out keyword) allows you to use a more derived type than specified by the generic parameter. Contravariance (in keyword) allows you to use a less derived type. They ensure type safety in generic interfaces and delegates.
Interview tip: Use a short example and connect the answer to real project work.
Quick revision checklist
- Explain the answer without reading.
- Write one small example.
- Review platform or version differences in official documentation.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.