What is a constructor in C#?
A constructor is a special member of a class that runs automatically when an object is created. It has the same name as the class and has no return type.
C# interview questions covering classes, constructors & properties.
Open a question for the full answer, code and interview guidance.
A constructor is a special member of a class that runs automatically when an object is created. It has the same name as the class and has no return type.
Common constructor forms include parameterless constructors, parameterized constructors, static constructors, and private constructors.
Constructor overloading means defining multiple constructors in the same class with different parameter lists. It allows callers to create objects using different sets of initialization data.
Constructor chaining means one constructor calls another constructor. The this keyword calls another constructor in the same class, while base calls a constructor in the base class.
Access modifiers determine where a type or member can be accessed.
A property provides accessor-based access to data exposed by a class or struct. Properties normally use get, set, or init accessors.
A field is a variable declared directly inside a class or struct. A property is a member that exposes access through get, set, or init accessors.
A static member belongs to the type itself rather than to an individual object instance. It can therefore be accessed through the type name without creating an object.
A sealed class cannot be inherited. It is useful when a type's behavior should not be extended through class inheritance.
A partial class allows one class definition to be split across multiple source files. At compile time, the parts are combined into a single type.