These are important topics for a Senior .NET Software Engineer interview. Let's understand each concept with simple C# examples, when to use it, and common interview questions.
1. Classes, Structs, Records, and Interfaces
1.1 Class
A class is a blueprint for creating objects. It can contain properties, fields, constructors, and methods.
Example: Imagine we're building an application that manages employees.
public class Employee
{
public string Name { get; set; }
public decimal Salary { get; set; }
public void DisplayInfo()
{
Console.WriteLine($"{Name} earns {Salary}");
}
}
Creating an object:
Employee employee = new Employee
{
Name = "Anish",
Salary = 80000
};
employee.DisplayInfo();
Key points:
- A class is a **reference **type.
- Multiple variables can reference the same object.
- Classes support inheritance from other classes.
- Classes can implement multiple interfaces.
For example, assigning one variable to another copies the reference, not the object itself.
Employee first = new Employee { Name = "Anish" };
Employee second = first;
second.Name = "Ram";
Console.WriteLine(first.Name); // Ram
Both variables refer to the same employee object.
When to use: Use a class for entities with identity, behavior, or mutable state, such as employees, orders, and customers.
1.2 Struct
A struct is a value type, commonly used to represent small values that belong together.
Example: Representing a 2D point.
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
}
Using it:
Point first = new Point { X = 10, Y = 20 };
Point second = first;
second.X = 100;
Console.WriteLine(first.X); // 10
Console.WriteLine(second.X); // 100
Unlike the class example, assigning first to second copies the struct's value. Changing one does not change the other.
Key points:
- A struct is a value type.
- Structs are copied by value in ordinary assignments.
- A struct cannot inherit from another class or struct.
- A struct can implement interfaces.
- Structs can have methods, properties, and constructors.
When to use: Use a struct for small, value-like data such as coordinates, measurements, or geometric points.
Note: Value type does not automatically mean that the data is stored on the stack. Actual storage depends on how the value is used.
1.3 Record
A record is a type designed to make working with data convenient, particularly when comparing values rather than object identities.
Example:
public record Employee(string Name, decimal Salary);
Creating records:
Employee first = new("Anish", 80000);
Employee second = new("Anish", 80000);
Console.WriteLine(first == second); // True
Why is the result True?
Because records use value-based equality by default. The two records have the same property values.
Compare that with classes:
public class EmployeeClass
{
public string Name { get; set; }
}
var first = new EmployeeClass { Name = "Anish" };
var second = new EmployeeClass { Name = "Anish" };
Console.WriteLine(first == second); // False
The two class instances are different objects, so their default equality comparison returns False.
Records also support creating modified copies with the with expression:
Employee first = new("Anish", 80000);
Employee second = first with { Salary = 90000 };
Console.WriteLine(first.Salary); // 80000
Console.WriteLine(second.Salary); // 90000
The original record remains unchanged in this example.
Key points:
- Records provide value-based equality by default.
recordcreates a reference type.record structcreates a value type.- Records support the
withexpression for non-destructive copying. - A record is not automatically deeply immutable; its members can still refer to mutable objects.
When to use: Use records for DTOs, API request and response models, and data that is naturally compared by its values.
1.4 Interface
An interface defines a contract that implementing types must fulfill.
Think of it as saying: "Any class implementing this interface must provide these operations."
Example:
public interface IPayment
{
void Pay(decimal amount);
}
A class implements the interface:
public class EsewaPayment : IPayment
{
public void Pay(decimal amount)
{
Console.WriteLine($"Paid Rs. {amount} using eSewa");
}
}
Another class can implement the same contract differently:
public class KhaltiPayment : IPayment
{
public void Pay(decimal amount)
{
Console.WriteLine($"Paid Rs. {amount} using Khalti");
}
}
Using the interface:
IPayment payment = new EsewaPayment();
payment.Pay(1000);
payment = new KhaltiPayment();
payment.Pay(2000);
Both payment methods can be used through the same interface, even though their implementations differ.
Key points:
- An interface defines a contract.
- Classes and structs can implement interfaces.
- A class can implement multiple interfaces.
- Interfaces enable loose coupling and dependency injection.
- Modern C# interfaces can also contain default implementations and static members, so an interface is not strictly limited to method declarations.
When to use: Use interfaces when different implementations need to provide the same functionality, such as payment gateways, logging services, and notification providers.
Quick comparison
| Feature | Class | Struct | Record | Interface |
|---|---|---|---|---|
| Main purpose | Objects and behavior | Value-like data | Data with value equality | Define a contract |
| Type category | Reference | Value | Reference by default | Contract/type |
| Value equality by default | No | Depends on value-type equality rules | Yes | No implementation of its own by default |
| Inheritance | From one class | No class inheritance | Record classes can inherit from records | Multiple interfaces |
| Can contain methods | Yes | Yes | Yes | Declarations and supported implementations |
| Common use | Employee, Order | Point, Coordinate | DTO, API model | Payment, Logger |
2. Abstract Classes vs. Interfaces
This is a particularly common .NET interview question.
2.1 Abstract class
An abstract class is a base class that cannot be instantiated directly. It can contain both implemented methods and abstract methods that derived classes must implement.
Example:
public abstract class Animal
{
public void Eat()
{
Console.WriteLine("Animal is eating");
}
public abstract void MakeSound();
}
A derived class implements the abstract method:
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark");
}
}
Using it:
Dog dog = new Dog();
dog.Eat(); // Animal is eating
dog.MakeSound(); // Bark
Notice that Dog inherits the Eat() implementation but supplies its own MakeSound() implementation.
When to use: Use an abstract class when related classes share common state or behavior, while some behavior must be customized by each subclass.
2.2 Interface
An interface defines a contract without requiring the implementing types to share a common base class.
public interface INotification
{
void Send(string message);
}
Different implementations can fulfill that contract:
public class EmailNotification : INotification
{
public void Send(string message)
{
Console.WriteLine($"Email: {message}");
}
}
public class SmsNotification : INotification
{
public void Send(string message)
{
Console.WriteLine($"SMS: {message}");
}
}
Both types can be used through INotification without inheriting from a shared notification base class.
INotification notification = new EmailNotification();
notification.Send("Your order has shipped");
notification = new SmsNotification();
notification.Send("Your order has shipped");
When to use: Use an interface when unrelated classes need to support the same operation or when you want to swap implementations easily.
2.3 Main differences
| Feature | Abstract class | Interface |
|---|---|---|
| Methods with implementations | Yes | Possible with default implementations |
| Instance fields and constructors | Yes | No instance fields or instance constructors |
| Abstract/unimplemented methods | Yes | Yes, through member declarations |
| Inheritance | A class can inherit one base class | A type can implement multiple interfaces |
| Shared state | Yes | No instance state |
| Access modifiers | Supports different access levels | Most ordinary contract members are public; some advanced interface members have other rules |
| Best suited for | Shared foundation and behavior | A capability or contract |
Practical example: Which should you choose?
Suppose you're building an e-commerce application.
EmailNotificationandSmsNotificationboth send notifications. UseINotificationto define their common contract.DogandCatare both animals, and they share common animal behavior. AnAnimalabstract class may be appropriate.EmployeeDtocontains data that you compare by its values. Arecordmay be appropriate.Pointrepresents a small value made up of coordinates. Astructmay be appropriate.
One important rule: you can combine abstract classes and interfaces. For example, an Animal abstract class can implement an IWalkable interface.
3. C# Interview Questions and Answers
Classes, Structs, Records, Interfaces, and Abstract Classes
These 20 common .NET interview questions cover definitions, differences, and practical scenarios.
1. Classes and Objects
Q1. What is a class in C#?
Answer: A class is a blueprint for creating objects. It defines the properties, fields, and methods that its objects can have.
public class Employee
{
public string Name { get; set; }
public void Work()
{
Console.WriteLine("Working...");
}
}
Q2. What is the difference between a class and an object?
Answer: A class is a blueprint, while an object is an instance of that class.
Employee employee = new Employee();
Here, Employee is the class, and employee refers to the created object.
Q3. Is a class a reference type or a value type?
Answer: A class is a reference type. When we assign one class variable to another, we copy the reference, so both variables can refer to the same object.
Employee first = new Employee { Name = "Ram" };
Employee second = first;
second.Name = "Hari";
Console.WriteLine(first.Name); // Hari
2. Structs
Q4. What is a struct in C#?
Answer: A struct is a value type used to represent data that behaves like a single value, such as a point, coordinate, or measurement.
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
}
Q5. What is the difference between a class and a struct?
Answer: A class is a reference type, whereas a struct is a value type. Assigning a class variable copies its reference, while assigning a struct normally copies its value.
| Class | Struct |
|---|---|
| Reference type | Value type |
| Can inherit from a class | Cannot inherit from another class or struct |
| Supports class inheritance | Can implement interfaces |
| Suitable for entities and complex objects | Suitable for value-like data |
Q6. Can a struct implement an interface?
Answer: Yes. A struct can implement one or more interfaces, even though it cannot inherit from another class or struct.
public interface IPrintable
{
void Print();
}
public struct Report : IPrintable
{
public void Print()
{
Console.WriteLine("Printing report");
}
}
3. Records
Q7. What is a record in C#?
Answer: A record is a type designed for data-centric scenarios. It provides value-based equality by default, meaning two records with the same relevant values can compare as equal.
public record Employee(string Name, int Age);
var first = new Employee("Ram", 25);
var second = new Employee("Ram", 25);
Console.WriteLine(first == second); // True
Q8. What is the difference between a class and a record?
Answer: A class uses reference identity for equality by default, while a record provides value-based equality by default. Records also support convenient non-destructive copying with the with expression.
public record Employee(string Name, int Age);
var first = new Employee("Ram", 25);
var second = first with { Age = 26 };
first.Age remains 25, while second.Age is 26.
Q9. Are records value types or reference types?
Answer: A record declared without struct is a reference type. A record struct is a value type.
public record Employee(string Name); // Reference type
public record struct Point(int X, int Y); // Value type
4. Interfaces
Q10. What is an interface in C#?
Answer: An interface defines a contract that implementing classes or structs must fulfill. It allows different types to expose common functionality through the same abstraction.
public interface IPayment
{
void Pay(decimal amount);
}
Q11. Can a class implement multiple interfaces?
Answer: Yes. C# allows a class to implement multiple interfaces, which is useful when a class needs to support multiple capabilities.
public class Printer : IPrintable, IDisposable
{
public void Print() { }
public void Dispose() { }
}
Here, Printer implements two interfaces.
Q12. Why do we use interfaces in .NET?
Answer: Interfaces reduce coupling by allowing code to depend on an abstraction rather than a specific implementation. They also make dependency injection, unit testing, and replacing implementations easier.
For example, a service can depend on IPayment instead of directly depending on EsewaPayment. We can then provide a different payment implementation without changing the service’s core logic.
Q13. Can an interface contain method implementations?
Answer: Yes. Modern C# supports default interface implementations. However, interfaces are still primarily used to define contracts, whereas abstract classes can share instance state and constructors.
public interface ILogger
{
void Log(string message)
{
Console.WriteLine(message);
}
}
This is a default implementation. A class can provide its own implementation if needed.
5. Abstract Classes vs. Interfaces
Q14. What is an abstract class?
Answer: An abstract class is a class that cannot be instantiated directly. It can contain implemented methods, abstract methods, properties, fields, and constructors.
public abstract class Animal
{
public void Eat()
{
Console.WriteLine("Eating");
}
public abstract void MakeSound();
}
Derived classes inherit the common behavior and implement the abstract members.
Q15. What is the difference between an abstract class and an interface?
Answer: An abstract class provides a shared base with reusable behavior and instance state. An interface defines a contract that different types can implement.
| Abstract class | Interface |
|---|---|
| Can contain instance fields | Cannot contain instance fields |
| Can have constructors | Cannot have instance constructors |
| Can provide implemented methods | Can provide default implementations |
| A class can inherit only one class | A type can implement multiple interfaces |
Q16. When would you use an abstract class instead of an interface?
Answer: I would use an abstract class when related classes share common state or implementation and need a common base. I would use an interface when different classes need to follow the same contract without requiring a shared base class.
Example: An Animal abstract class can share common animal behavior, while an INotification interface can be implemented by email, SMS, and push notification services.
Q17. Can an abstract class implement an interface?
Answer: Yes. An abstract class can implement an interface and provide some of its implementations. Concrete derived classes must fulfill any remaining interface requirements.
public interface IWorker
{
void Work();
}
public abstract class Employee : IWorker
{
public abstract void Work();
public void TakeBreak()
{
Console.WriteLine("Taking a break");
}
}
Q18. Can we create an object of an abstract class?
Answer: No. We cannot instantiate an abstract class directly. However, we can create an object of a concrete derived class and reference it through the abstract base type.
public abstract class Animal { }
public class Dog : Animal { }
Animal animal = new Dog(); // Valid
new Animal() would be invalid.
6. Scenario-Based Questions
Q19. You have Email, SMS, and Push notification services. Would you use an abstract class or an interface? Why?
Answer: I would generally use an INotification interface because each service needs to follow the same contract, but the implementations are different. This also makes it easy to inject or replace notification services.
public interface INotification
{
void Send(string message);
}
If all notification services later need substantial shared implementation or state, an abstract base class could complement the interface.
Q20. You need to create an API response model in ASP.NET Core. Would you use a class or a record?
Answer: I would consider a record when the response is primarily data and value-based equality or convenient copying is useful. I would use a class when mutable state, identity-based behavior, or framework-specific requirements make it a better fit.
public record EmployeeResponse(
int Id,
string Name,
string Email);
Both classes and records can be used as ASP.NET Core DTOs. The choice depends on the model’s intended behavior.
Quick Revision
- Class: An object with identity, behavior, and potentially mutable state.
- Struct: A value type for value-like data.
- Record: A type with value-based equality by default, useful for data models.
- Interface: A contract that implementations agree to fulfill.
- Abstract class: A shared base that can provide common behavior while requiring subclasses to implement certain operations.
Interview Tip
Practice answering each question aloud in 30–60 seconds. For senior-level interviews, add a practical example from ASP.NET Core, dependency injection, or unit testing to explain why you would choose one type over another.
Share this article