C# Interview Preparation
# CHAPTER 28
C# Interview Preparation
1. Introduction
Congratulations on completing the technical coursework! You now understand C# from basic syntax to enterprise-level frameworks like ASP.NET Core and Entity Framework. This chapter consolidates that knowledge into high-yield interview questions designed to prepare you for junior to mid-level backend developer interviews.2. Learning Objectives
By the end of this chapter, you will be able to:- Answer the most frequent Core C# theoretical questions.
- Explain OOP principles clearly.
-
Discuss advanced topics like
async/awaitand LINQ.
- Understand common C# white-board coding challenges.
3. Core C# & .NET Interview Questions
1. What is the difference between Managed and Unmanaged code? *Answer:* Managed code runs inside the .NET Common Language Runtime (CLR), which handles memory management (Garbage Collection), security, and type safety. Unmanaged code runs directly on the OS hardware (like C or C++), requiring manual memory management.
2. Explain the difference between Value Types and Reference Types.
*Answer:* Value Types (like int, bool, struct) hold their data directly and are stored on the Stack. Reference Types (like string, class, array) store a memory address on the Stack that points to the actual data stored on the Heap.
3. What is the difference between String and StringBuilder?
*Answer:* String is immutable; every time you modify it, a new string is created in memory, which is terrible for performance in loops. StringBuilder is mutable and modifies the string buffer in-place, making it highly efficient for heavy text manipulation.
4. What is the var keyword? Is it dynamically typed?
*Answer:* No, var is strongly and statically typed. It simply tells the compiler to deduce the data type based on the right side of the assignment at compile-time. Once the type is set, it cannot be changed.
4. OOP Interview Questions
5. Name and define the 4 Pillars of OOP. *Answer:*
- Encapsulation: Hiding internal state and requiring interaction through controlled properties/methods.
- Abstraction: Hiding complex implementation details behind simple interfaces.
- Inheritance: Creating child classes that inherit fields/methods from parent classes.
- Polymorphism: Allowing a single method name to behave differently (Overriding) or treating derived objects as base objects.
6. What is the difference between an Interface and an Abstract Class? *Answer:* A class can inherit only one Abstract Class, but can implement multiple Interfaces. Abstract classes define core identity ("Is-A") and can contain state (fields). Interfaces define capabilities ("Can-Do") and cannot contain state.
7. What is Method Overloading vs Method Overriding?
*Answer:* Overloading (Compile-time) is multiple methods with the same name but different parameters in the same class. Overriding (Runtime) is a child class replacing a parent's virtual method using the override keyword.
5. Advanced C# Interview Questions
8. Explain async and await. Does it create a new thread?
*Answer:* async/await enables asynchronous programming to keep threads responsive (especially UI threads or Web Server threads) during I/O operations (like database calls). It does NOT inherently create a new OS thread; it uses a state machine to release the current thread back to the Thread Pool while waiting, and resumes when the task completes.
9. What is LINQ? Explain Deferred Execution.
*Answer:* Language-Integrated Query (LINQ) provides SQL-like querying capabilities directly in C#. Deferred Execution means a LINQ query is not actually executed against the data source when it is defined, but only when the data is explicitly iterated over (e.g., via a foreach loop or calling .ToList()).
10. What is Dependency Injection (DI)?
*Answer:* A design pattern where an object receives its dependencies from an external source rather than creating them itself (using new). It promotes loose coupling and makes code highly testable. It is a core, built-in feature of ASP.NET Core.
6. Common Coding Challenges
You may be asked to write code on a whiteboard or a shared screen.Challenge 1: String Reversal (Without built-in methods)
Challenge 2: LINQ Filtering
*Task:* Given a List<int>, return a list containing only the even numbers, sorted descending.
7. Common Mistakes in Interviews
-
Using
ArrayList: If asked to store data dynamically, never sayArrayList. Always useList<T>. MentioningArrayListshows outdated knowledge.
-
Confusing
overridewithnew: Thenewkeyword hides a base method, whileoverrideactually replaces it for runtime polymorphism. Always preferoverride.
-
Not mentioning
usingblocks: If asked to read a file or connect to a database, failing to wrapStreamReaderorSqlConnectionin ausingblock is an instant red flag regarding your memory management skills.
8. Best Practices Checklist
- Ensure you understand the difference between C# (the language), .NET (the runtime/framework), and ASP.NET Core (the web framework).
- Be prepared to discuss SOLID principles, specifically the Single Responsibility Principle and Dependency Inversion.
9. Exercises
- 1. Hand-write the answers to the 10 theoretical questions above.
- 2. Open an IDE and write a C# program that implements the FizzBuzz challenge: Print numbers 1 to 100. If divisible by 3, print "Fizz". Divisible by 5, print "Buzz". Divisible by both, print "FizzBuzz".
10. MCQs with Answers
Which of the following runs inside the CLR and benefits from Garbage Collection?
Which keyword is used to deduce the type of a variable at compile-time?
var dynamically typed?
a) Yes, you can change the type later b) No, it is strongly typed and locked in at compile-time
Answer: b) No, it is strongly typed
Which of the following is terrible for memory when modifying text inside a large loop?
A class implementing multiple interfaces solves C#'s lack of what feature?
What happens when an async method hits an await operator?
When is a LINQ query typically executed?
Which of the following ensures an object implementing IDisposable (like a database connection) is safely closed?
Which OOP pillar does Method Overriding fall under?
What design pattern does ASP.NET Core heavily rely on to provide database contexts and logging services to controllers?
11. Interview Questions
-
Q: Explain the Garbage Collector. Can you force it to run? (Answer: Yes, via
GC.Collect(), but it is highly discouraged as it disrupts the CLR's optimized schedule).
-
Q: What is the difference between an
IEnumerableand anIQueryablein Entity Framework?