OOP
Classes and objects, constructors and chaining, access modifiers, abstraction, and abstract classes.
6 questions
JuniorTheoryVery commonWhat is a constructor in C#, and when is a default one provided?
What is a constructor in C#, and when is a default one provided?
A constructor is a special method that runs when an object is created with new, initializing the new instance's fields. It shares the class name and has no return type. If you declare no constructor, the compiler supplies a public parameterless default; once you declare any constructor that default disappears. Constructors can be overloaded by parameter list.
Common mistakes
- ✗Thinking the compiler keeps the default constructor after you declare your own
- ✗Believing a constructor has a return type or is called by name after
new - ✗Assuming a class can have only one constructor and cannot overload it
Follow-up questions
- →What initializes a field if no constructor explicitly assigns it?
- →How does the compiler pick which overloaded constructor
newinvokes?
JuniorTheoryCommonWhat is the difference between a class and an object in C#?
What is the difference between a class and an object in C#?
A class is a reference-type blueprint defining the state (fields) and behavior (methods) a kind of thing has. An object is a concrete instance of that class, allocated on the heap with new, holding its own field values. One class describes many objects; the variable holds a reference to the object, not the object itself.
Common mistakes
- ✗Confusing the class (the definition) with the object (a runtime instance of it)
- ✗Believing a class variable stores the object inline rather than a reference to it
- ✗Assuming all objects of a class share one set of field values
Follow-up questions
- →What happens to two variables that are assigned the same object reference?
- →Where do an object's fields live versus where the reference variable lives?
MiddleTheoryCommonWhen do you choose an abstract class over an interface in C#?
When do you choose an abstract class over an interface in C#?
An abstract class is one base you can inherit, and it may carry implementation, fields, and a constructor — modeling an "is-a" relationship with shared state. An interface is a pure contract (no instance state) that many types can implement, modeling a "can-do" capability. Choose an abstract class to share code/state among close relatives; choose an interface for a capability across unrelated types.
Common mistakes
- ✗Believing an abstract class cannot hold implementation, fields, or a constructor
- ✗Thinking a type can inherit several abstract classes the way it implements many interfaces
- ✗Trying to instantiate an abstract class directly with
new
Follow-up questions
- →Since C# added default interface methods, when is an abstract class still required?
- →How does an abstract method differ from a
virtualmethod with a body?
MiddleTheoryCommonWhat are C#'s access modifiers, and what is the default for class members?
What are C#'s access modifiers, and what is the default for class members?
C# has six: public (everywhere), private (same type only), protected (the type and its derivatives), internal (same assembly), protected internal (same assembly OR a derived type — the union), and private protected (same assembly AND a derived type — the intersection). A class member with no modifier defaults to private; a top-level type defaults to internal.
Common mistakes
- ✗Listing only four modifiers and missing
protected internal/private protected - ✗Swapping the OR (union) and AND (intersection) meanings of the combined modifiers
- ✗Assuming an unmarked class member defaults to
publicrather thanprivate
Follow-up questions
- →Why is
protected internalwider in reach thanprivate protected? - →What is the default accessibility of a top-level class versus a nested one?
MiddleTheoryOccasionalHow do : this(...) and : base(...) chain constructors in C#?
How do : this(...) and : base(...) chain constructors in C#?
A constructor initializer chains setup before the body runs. : this(...) forwards to another constructor in the SAME class, centralizing shared init. : base(...) calls a specific BASE-class constructor. Construction runs base-first: the base constructor (explicit or the implicit parameterless base()) completes before the derived body executes, so derived code sees an initialized base.
Common mistakes
- ✗Swapping the meanings of
: this(...)and: base(...) - ✗Thinking the derived body runs before the base constructor completes
- ✗Believing
: this(...)can forward to a constructor in another class
Follow-up questions
- →If you write no
: base(...), which base constructor does the compiler call? - →Can two constructors chain to each other with
: this(...)and form a cycle?
SeniorTheoryOccasionalWhat is a static constructor in C#, and when does it run?
What is a static constructor in C#, and when does it run?
A static constructor initializes a type's static state. It takes no parameters and no access modifier, and you never call it directly. The runtime runs it automatically exactly ONCE per type, lazily, just before the first instance is created or any static member is accessed. So it may run before or after Main depending on when the type is first touched.
Common mistakes
- ✗Thinking a static constructor takes parameters, a modifier, or can be called directly
- ✗Believing it runs per instance or per thread rather than once per type
- ✗Assuming it always runs before
Mainregardless of when the type is first used
Follow-up questions
- →What happens to a type if its static constructor throws an exception?
- →How does
beforefieldinitchange exactly when the static constructor fires?