C# OOPs Interview Questions
Last updated 2026-11-19
Object-oriented questions in C# lean on language specifics: the difference between override and new, how explicit interface implementation works, what the access modifiers mean exactly, and why a struct cannot inherit. Output-prediction puzzles about virtual calls are the favourite.
These answers target C# 12. For the language-neutral concepts behind them, such as SOLID and design patterns, see OOPs interview questions. General language topics are on the C# interview questions page.
| Language version | C# 12 on .NET 8 |
|---|---|
| Pillars | Encapsulation, inheritance, polymorphism, abstraction |
| Level | Fresher to 4 years |
C# rules that differ from what you may know from Java
- Methods are non-virtual by default; you opt in with virtual and opt in again with override.
- Derived field initialisers run before the base constructor body.
- Interfaces can have default implementations, but calling them requires an interface-typed reference.
- Properties, indexers and events are first-class members, not naming conventions.
C# OOPs Interview Questions: questions with worked answers
1 Explain the four pillars of OOP with C# examples.
easyEncapsulation hides data behind members, for example a private field with a property that validates writes. Inheritance reuses a base class with the colon syntax (class Dog : Animal). Polymorphism lets a base reference call derived behaviour through virtual and override or through interfaces. Abstraction exposes essential operations through abstract classes and interfaces while hiding implementation.
2 Predict the output: virtual and override versus new.
mediumoverride replaces the base implementation and dispatches on the runtime type. new only hides the base member, so the call depends on the compile-time type of the reference. The same object therefore prints different text through different variable types.
csharpclass Base { public virtual void A() => Console.WriteLine("Base.A"); public virtual void B() => Console.WriteLine("Base.B"); } class Derived : Base { public override void A() => Console.WriteLine("Derived.A"); public new void B() => Console.WriteLine("Derived.B"); } Base x = new Derived(); x.A(); // Derived.A (override) x.B(); // Base.B (hidden, not overridden) ((Derived)x).B(); // Derived.B3 What is the difference between abstract class and interface in C# 12?
mediumAn abstract class can have fields, constructors, any access level and both abstract and concrete members, and a class inherits only one. An interface defines a contract with no instance fields; since C# 8 it can include default implementations, static members (including abstract static members since C# 11) and private helpers, and a type can implement many interfaces. Use an interface for a capability and an abstract class for shared state with partial implementation.
4 How does explicit interface implementation work, and when is it needed?
mediumWriting void IA.Run() implements the member only for the interface, with no access modifier, and it is invisible on the class type, so you must cast to the interface to call it. It resolves name clashes between two interfaces and hides rarely used members from the public surface.
csharpinterface IA { void Run(); } interface IB { void Run(); } class Both : IA, IB { void IA.Run() => Console.WriteLine("A"); void IB.Run() => Console.WriteLine("B"); } var b = new Both(); ((IA)b).Run(); // A ((IB)b).Run(); // B // b.Run(); // compile error5 List the access modifiers and what private protected and file mean.
easypublic is visible everywhere, private only inside the type, protected inside the type and derived types, internal inside the same assembly, protected internal is protected or internal, and private protected is protected and internal, so only derived types in the same assembly. file (C# 11) limits a type to its own source file, handy for source generators.
6 What does sealed do?
easyA sealed class cannot be inherited and a sealed override cannot be overridden further. It documents intent, enables some JIT devirtualisation, and is the default choice for classes not designed for inheritance. String and most value types are sealed.
7 How do constructors chain in C#?
mediumA constructor can call another one in the same class with : this(...) or a base constructor with : base(...). If you omit it the compiler calls the parameterless base constructor, which must exist and be accessible. Static constructors run once before the first use of the type and take no parameters or modifiers.
csharpclass Vehicle { public string Name { get; } public Vehicle(string name) { Name = name; } } class Car : Vehicle { public int Doors { get; } public Car(string name, int doors) : base(name) { Doors = doors; } public Car(string name) : this(name, 4) { } } Console.WriteLine(new Car("Swift").Doors); // 48 What does this print? Calling a virtual method from a base constructor.
hardThe runtime type is already Derived while the base constructor runs, so the derived override executes. In C# the derived field initialiser has already run, so the field holds its initial value, while a value assigned in the derived constructor body is not yet set and shows its default.
csharpclass P { public P() { Show(); } public virtual void Show() => Console.WriteLine("P"); } class Q : P { string tag = "init"; string late; public Q() { late = "ctor"; } public override void Show() => Console.WriteLine($"Q {tag} {late ?? "null"}"); } new Q(); // Q init null9 Properties versus fields: what is encapsulation done with properties?
mediumA property is a pair of accessor methods that looks like a field. It lets you validate on set, compute on get, make a value read-only, give set and get different accessibility or change internals later without breaking callers. Auto-properties generate a hidden backing field, init accessors allow setting only during object initialisation and required members force callers to set them.
csharppublic class Account { private decimal _balance; public decimal Balance { get => _balance; private set => _balance = value >= 0 ? value : throw new ArgumentOutOfRangeException(nameof(value)); } public required string Owner { get; init; } public void Deposit(decimal amt) => Balance += amt; }10 What are the differences between struct and class regarding OOP?
mediumA struct is a value type: it cannot inherit from another struct or class (it can implement interfaces), it has no finaliser, copies on assignment, and its parameterless constructor cannot be removed (C# 10 allows custom ones). It is best for small immutable data of up to about 16 bytes. Interface calls on a struct box it unless a constrained generic is used.
11 How do you override Equals and GetHashCode, and why implement IEquatable<T>?
mediumEqual objects must produce equal hash codes or Dictionary and HashSet will misbehave. Implementing IEquatable<T> gives a strongly typed Equals that avoids boxing for structs. HashCode.Combine builds a good hash from several fields. Records generate all of this automatically.
csharppublic sealed class Point : IEquatable<Point> { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public bool Equals(Point? o) => o is not null && X == o.X && Y == o.Y; public override bool Equals(object? o) => Equals(o as Point); public override int GetHashCode() => HashCode.Combine(X, Y); }12 Explain polymorphism through interfaces with a short example and mention Liskov substitution.
mediumCallers depend on the interface, and each implementation supplies its own behaviour, so adding a new implementation needs no change to the caller. Liskov substitution says that any derived type must be usable wherever the base is expected without surprising callers, for example a ReadOnlyCollection that throws on Add breaks the contract of a mutable one.
csharpinterface IShape { double Area(); } record Circle(double R) : IShape { public double Area() => Math.PI * R * R; } record Rect(double W, double H) : IShape { public double Area() => W * H; } IShape[] shapes = [new Circle(1), new Rect(2, 3)]; Console.WriteLine(shapes.Sum(s => s.Area())); // 9.14159...13 What is an abstract property, and can an abstract class have a constructor?
easyYes to both. An abstract property declares an accessor shape that derived classes must implement. An abstract class can have constructors, called through base(...) from derived constructors; they are usually protected. You cannot create an instance of the abstract type itself.
14 What are operator overloading and indexers?
mediumOperator overloading defines how operators such as +, == and < behave for your type through public static operator methods; if you overload == you must also overload != and should override Equals and GetHashCode. An indexer lets an object be accessed like an array with this[int i] and can have multiple parameters.
csharppublic readonly record struct Money(decimal Amount) { public static Money operator +(Money a, Money b) => new(a.Amount + b.Amount); } Console.WriteLine(new Money(5) + new Money(7)); // Money { Amount = 12 }15 What is the difference between composition and inheritance, and which does C# favour?
mediumInheritance models is-a and ties the child to the parent implementation, so changes to the base can break derived classes. Composition models has-a by holding another object and delegating to it, giving flexibility and easier testing with dependency injection. Favour composition and interfaces, using inheritance for true specialisations or to share a small stable base. Sealed classes and primary constructors make composition cheaper to write.
16 How does C# handle the diamond problem with default interface members?
hardA class can implement two interfaces with a default member of the same name, but it must provide its own implementation, otherwise a call through the class is ambiguous and the compiler reports an error. Unlike Java, there is no InterfaceName.super syntax; you call a default implementation only through a reference of the interface type.
csharpinterface IA { void Hi() => Console.WriteLine("A"); } interface IB { void Hi() => Console.WriteLine("B"); } class C : IA, IB { void IA.Hi() => Console.WriteLine("C via IA"); void IB.Hi() => Console.WriteLine("C via IB"); } ((IA)new C()).Hi(); // C via IA
Frequently asked questions
What is the most asked C# OOP question?
The difference between override and new, followed by abstract class versus interface and what sealed does.
Does C# support multiple inheritance?
Not of classes. A class inherits from one class and implements many interfaces.
What is the difference between virtual and abstract?
A virtual method has a default body that derived classes may override; an abstract method has no body and must be overridden in concrete derived classes.
Which page covers general object-oriented design?
See OOPs interview questions for SOLID and patterns.
Related guides
More in Programming Language Interview Questions by Language
- C++ Coding Interview Questions
- C++ Interview Questions and Answers
- C++ Memory Management Interview Questions
- C++ OOPS Interview Questions
- C++ Pointers and References Interview Questions
- C++ Smart Pointers Interview Questions
- C++ STL Interview Questions
- C++ Templates Interview Questions
- C++ Virtual Functions Interview Questions
- Core Java Interview Questions for Freshers
- Entity Framework Core Interview Questions
- Java 8 Streams and Lambdas Interview Questions