C# ClassesLesson 27Free preview
C# Constructors
This lesson is part of C# · All levels
Summary
A constructor is a special method that runs automatically when an object is created with new, and the name is exactly the same as the name of the class. When there's no constructor written, C# makes the default constructor without parameters. The constructors can overload and call each other with this or base syntax to avoid duplication of code.
Key points
- The constructor has no type return and its name is exactly the same as the class name.
- The automatic default constructor is available when the class does not define any constructor.
- Parametric constructors allow objects to be directly filled with data as they are created.
- Constructor chaining uses this(...) to call another constructor in the same class or base(...) for the parent constructor.
- Static constructors run once when the class is first used.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.