Classes, Properties, and Constructors
Defining classes, auto-implemented properties, and constructors.
What you'll learn
- Define a class with fields, auto-implemented properties, and methods
- Write a constructor that initializes an object's properties
- Explain what an auto-implemented property (`{ get; set; }`) generates behind the scenes
Explanation
C# is built around classes: public class Person { ... } defines a custom type combining data and behavior. You create an instance with new: var ada = new Person("Ada", 30);.
A property looks like a field from the outside (ada.Name) but is backed by get/set accessor logic. The shorthand public string Name { get; set; } is an auto-implemented property -- the compiler generates a hidden private backing field and trivial get/set accessors for you. You can also write a full property with custom logic in the accessors when you need validation or computed values, e.g. rejecting a negative age in the set accessor.
A constructor is a special method matching the class name with no return type, run automatically when you create an instance with new: public Person(string name, int age) { Name = name; Age = age; }. If you don't declare any constructor, C# generates a public parameterless one for you automatically -- but as soon as you declare any constructor yourself, that automatic one disappears unless you also declare a parameterless one explicitly.
Methods are declared inside the class body much like top-level functions, but implicitly operate on the specific instance they were called on (accessible explicitly via the this keyword, though it's often omitted when there's no ambiguity).
Guided lab
Predict: A class with properties and a constructor
Read this program and predict exactly what it prints.
using System;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
public string Greet()
{
return $"Hi, I'm {Name} and I'm {Age} years old.";
}
}
Person ada = new Person("Ada", 30);
Console.WriteLine(ada.Greet());
ada.Age = 31;
Console.WriteLine($"{ada.Name} is now {ada.Age}.");Stuck? Get a hint.
Common mistakes
- Assuming a class automatically gets a public parameterless constructor even after you've declared a different constructor yourself -- once you declare any constructor, the automatic parameterless one is gone unless you add it explicitly.
- Confusing an auto-implemented property (`{ get; set; }`) with a plain public field -- a property can later add validation logic without breaking calling code, while a field cannot.
- Forgetting that a constructor has no return type at all -- not even `void` -- unlike an ordinary method.
Knowledge check
Takeaway
Prefer auto-implemented properties (`{ get; set; }`) over plain public fields, since they let you add validation logic later without breaking any calling code -- and remember declaring any constructor removes the automatic parameterless one.
Summary
Classes combine data and behavior; auto-implemented properties generate a hidden backing field and simple accessors; a constructor (no return type) initializes a new instance created with `new`.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.