C# Fundamentals - Detailed Interview Guide
This document provides an in-depth explanation of C# fundamentals, tailored for interview
preparation. It goes beyond summaries, offering detailed explanations, examples, and interview tips
for each concept.
1. Overview of C#
C# (pronounced 'C-sharp') is a modern, object-oriented programming language developed by
Microsoft as part of its .NET initiative. It is designed for building a variety of applications, from
desktop to web and mobile. C# combines the power of C++ with the simplicity of Visual Basic, and
runs on the .NET runtime (CLR - Common Language Runtime). Key features include strong typing,
garbage collection, versioning support, and rich standard libraries.
2. Data Types in C#
C# supports value types and reference types. Value types (int, float, bool, struct) store data directly,
while reference types (class, interface, array, string) store a reference to the actual data. Interview
Tip: Understand how memory is allocated for value vs reference types.
3. Variables and Constants
Variables store data in memory and can change during execution. Constants (const) are fixed at
compile time. Readonly variables can be assigned once, either at declaration or in the constructor.
4. Control Statements
Includes conditional statements (if, else, switch) and looping constructs (for, while, do-while,
foreach). Interviewers may ask for edge cases such as infinite loops or fall-through behavior in
switch.
5. Methods, Parameters, out, and ref
Methods in C# define reusable blocks of code. Parameters can be passed by value (default), by
reference (ref), or as output (out). - ref: Requires the variable to be initialized before passing. - out:
Does not require initialization; the method must assign a value before returning.
6. Collections and Dictionaries
Collections store groups of related objects. Dictionaries store key-value pairs with fast lookup (O(1)
average time). Example: Dictionary ages = new Dictionary { {"Alice", 30}, {"Bob", 25} };
7. Hashing Concept
Hashing transforms input data into a fixed-size value (hash code). In dictionaries, hashing is used to
quickly locate keys. Good hashing minimizes collisions (different keys having the same hash code).
8. File Handling
File handling in C# uses the System.IO namespace. Key classes: - File: For creating, reading,
writing, and deleting files. - StreamReader/StreamWriter: For reading/writing text. - FileStream: For
reading/writing bytes.
9. Exception Handling
C# uses try-catch-finally blocks to handle runtime errors gracefully. Example: try { int x =
int.Parse("abc"); } catch (FormatException ex) { Console.WriteLine(ex.Message); } finally {
Console.WriteLine("Cleanup code here."); }
10. OOP Principles
C# supports the four main OOP principles: Encapsulation, Inheritance, Polymorphism, and
Abstraction. Interview Tip: Be ready to give real-world analogies and code examples for each.