C# Constructors
A constructor runs when an object is created and sets up its fields. It has the class name and no return type. Primary constructors (C# 12) put parameters right on the class.
Example
class Point
{
public int X, Y;
public Point(int x, int y) { X = x; Y = y; }
}
var p = new Point(3, 4);
Console.WriteLine(p.X + "," + p.Y);Exercise
Constructor name matches the ___.
class Car { public () {} }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.