Introduction
Preparing for a C# interview? Whether you're a beginner or an experienced developer, interviewers often test your understanding of core C# concepts rather than just syntax.
In this article, we'll cover 10 commonly asked C# interview questions with simple explanations and practical examples to help you prepare with confidence.
1. What Is the Difference Between Class and Struct?
A class is a reference type, while a struct is a value type.
| Class | Struct |
|---|---|
| Reference type | Value type |
| Stored on the heap | Usually stored on the stack or inline within another object |
| Supports inheritance | Does not support class inheritance |
| Can be null | Cannot be null unless declared as nullable |
Example
class Employee
{
public string Name { get; set; } = string.Empty;
}
struct Point
{
public int X;
public int Y;
}
Interview Tip: Use a struct for small, immutable data objects and a class for complex business objects.
2. What Is the Difference Between == and .Equals()?
==compares values for value types and, by default, object references for reference types (unless the operator is overloaded)..Equals()compares object equality and can be overridden to implement custom comparison logic.
Example
string a = "Hello";
string b = "Hello";
Console.WriteLine(a == b); // True
Console.WriteLine(a.Equals(b)); // True
Interview Tip: Custom classes can override .Equals() to compare object values instead of object references.
3. What Is the Difference Between ref, out, and in?
ref
The variable must be initialized before it is passed to the method.
void Increment(ref int number)
{
number++;
}
out
The variable does not need to be initialized, but the called method must assign a value before returning.
bool GetAge(out int age)
{
age = 25;
return true;
}
in
The parameter is passed by reference but is read-only inside the method.
void Display(in int value)
{
Console.WriteLine(value);
}
Remember:
ref= Read and writeout= Output onlyin= Read-only reference
4. What Is the Difference Between string and StringBuilder?
string objects are immutable, meaning every modification creates a new string instance.
StringBuilder objects are mutable and allow efficient string modifications.
Example
using System.Text;
StringBuilder builder = new StringBuilder();
builder.Append("Hello ");
builder.Append("World");
Console.WriteLine(builder.ToString());
Best Practice: Use StringBuilder when repeatedly modifying strings, especially inside loops.
5. What Are Boxing and Unboxing?
Boxing
Converting a value type into an object.
int number = 100;
object obj = number;
Unboxing
Converting an object back into its original value type.
int value = (int)obj;
Note: Excessive boxing and unboxing can negatively impact application performance because they involve additional memory allocations and type conversions.
6. What Is the Difference Between IEnumerable and IQueryable?
IEnumerable
Works with in-memory collections.
Filtering occurs after the data has been loaded into memory.
IQueryable
Builds database queries.
Filtering and projection occur in the database.
Join the conversation! Your thoughts help the community grow.