100 C# Interview Questions & Answers
This covers the C#-language-level ground an interview loop draws from — data types, the CLR's memory model, delegates/events/generics, collections, async internals, and concurrency primitives. A few questions here overlap with OOP principles or Dependency Injection specifically — where that's the case, this gives the C# angle and points to the dedicated 52 OOP interview questions and 35 Dependency Injection interview questions posts for the deeper design-level treatment.
C# Fundamentals
Q1. What is C# and what are its key features?
C# is Microsoft's statically-typed, object-oriented language running on .NET — type-safe, garbage-collected, and compiled to intermediate language (IL) that the CLR JIT-compiles to native code at runtime. Key features: full OOP support, generics, LINQ, async/await, pattern matching, and (since C# 8) nullable reference types for compile-time null-safety checking.
Q2. Explain the basic structure of a C# program.
// Modern C# (top-level statements, .NET 6+) — no explicit Main() needed
Console.WriteLine("Hello, world!");
// Classic structure:
namespace MyApp;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
}
}Q3. What are the different types of data types available in C#?
| Category | Examples |
|---|---|
| Value types | int, double, bool, char, struct, enum |
| Reference types | class, string, array, interface, delegate |
| Nullable value types | int?, DateTime? — via Nullable<T> |
Q4. What is the difference between value types and reference types?
struct Point { public int X, Y; }
Point p1 = new Point { X = 1 };
Point p2 = p1; // COPIES the whole struct
p2.X = 99; // p1.X is still 1
class Box { public int X; }
Box b1 = new Box { X = 1 };
Box b2 = b1; // COPIES the reference, not the object
b2.X = 99; // b1.X is now 99 too — same object| Value type | Reference type | |
|---|---|---|
| Storage | Typically the stack (or inline within a containing object) | The heap, referenced by a pointer stored on the stack |
| Assignment | Copies the entire value | Copies the reference — both variables point to the same object |
| Default | struct, int, bool, enum | class, string, array, delegate |
Q5. What are nullable types in C#?
int? age = null; // Nullable<int>
if (age.HasValue) Console.WriteLine(age.Value);
int actual = age ?? 0; // null-coalescing defaultValue types can't natively hold null (a plain int always has some numeric value) — Nullable<T> (int? is shorthand for Nullable<int>) wraps a value type to add "no value" as a legitimate state, which is essential for things like an optional database column mapped to a C# property.
Q6. Can you describe what namespaces are and how they are used in C#?
namespace MyCompany.Orders;
using System.Collections.Generic; // brings another namespace's types into scopeA namespace organizes types hierarchically and prevents naming collisions — two classes named Order can coexist as MyCompany.Orders.Order and ThirdParty.Sdk.Order without conflict, and a using directive avoids having to fully qualify every reference to a type from another namespace.
Q7. Explain the concept of boxing and unboxing in C#.
int i = 42;
object boxed = i; // boxing — the value type is wrapped in a heap object
int unboxed = (int)boxed; // unboxing — copied back out into a value typeBoxing wraps a value type in a heap-allocated object so it can be treated as object (or an interface it implements) — necessary because value types don't naturally live on the heap, but it costs an allocation and a copy. Unboxing reverses it. Both are a real, sometimes-overlooked performance cost when they happen inside a hot loop (a common source of unexpected allocations pre-generics, e.g. adding ints to an ArrayList).
Q8. What is Type Casting and what are its types in C#?
| Cast type | Example | Fails how |
|---|---|---|
| Implicit | int i = 5; long l = i; | Never — only allowed when no data can be lost |
| Explicit | double d = 5.7; int i = (int)d; | May lose data (truncates 5.7 to 5) silently |
| as | obj as string | Returns null on failure instead of throwing |
| is | if (obj is string s) | Pattern-matches and safely extracts the typed value |
Q9. What are operators in C# and can you provide examples?
| Category | Examples |
|---|---|
| Arithmetic | +, -, *, /, % |
| Comparison | ==, !=, <, >, <=, >= |
| Logical | &&, ||, ! |
| Null-related | ??, ??=, ?., ! |
| Bitwise | &, |, ^, ~, <<, >> |
Q10. What is the difference between == operator and .Equals() method?
string a = "hello";
string b = "hel" + "lo";
Console.WriteLine(a == b); // true — string overrides == for value comparison
Console.WriteLine(a.Equals(b)); // true — same reason
object x = new object();
object y = new object();
Console.WriteLine(x == y); // false — default == is reference comparison for objectFor reference types, == defaults to reference equality unless the type overloads it (string and records both do, for value-based comparison) — .Equals() is virtual and can be overridden per type for genuine content-based equality, which is why user-defined classes should override both consistently if value equality is intended.
Enjoyed this?
Let's talk about building something together.