Object-Oriented Programming Lesson 11 of 15
C# Classes, Objects, Properties and Constructors
Design C# classes with fields, properties, constructors and methods, create objects with new, protect data with private fields and validated properties, and use records.
A class is a blueprint for objects that bundle data and behavior. C# adds a very handy feature to classes: properties, which look like fields but can control how values are read and changed.
A class with properties and a constructor
var a = new Student("Maria", 11);
var b = new Student("Jose", 12);
a.AddScore(94);
a.AddScore(90);
b.AddScore(88);
Console.WriteLine(a);
Console.WriteLine(b);
class Student
{
public string Name { get; }
public int GradeLevel { get; set; }
private readonly List<int> scores = new();
public Student(string name, int gradeLevel)
{
Name = name;
GradeLevel = gradeLevel;
}
public void AddScore(int score) => scores.Add(score);
public double Average => scores.Count == 0 ? 0 : scores.Average();
public override string ToString() => $"{Name} (Grade {GradeLevel}): {Average:F2}";
}
Output
Maria (Grade 11): 92.00 Jose (Grade 12): 88.00
- In a top-level program, classes go after the top-level code.
public string Name { get; }is a read-only auto-property: it can only be set in the constructor.{ get; set; }can be read and changed from outside.public double Average => ...is a computed property, recalculated each time it's read.- The constructor has the class name and no return type; it runs with
new. override ToString()controls how the object prints.
Validated properties
A property can have a body that checks values, protecting the object from bad data:
var kape = new Product("Kape 3-in-1", 8m);
kape.Stock = 50;
try
{
kape.Stock = -5;
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine($"{kape.Name}: {kape.Stock} in stock");
class Product
{
private int stock;
public string Name { get; }
public decimal Price { get; }
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
public int Stock
{
get => stock;
set
{
if (value < 0) throw new ArgumentException("Stock can't be negative.");
stock = value;
}
}
}
Output
Stock can't be negative. Kape 3-in-1: 50 in stock
Inside a setter, value is the new value being assigned. The private field stock holds the real data; the public property guards it. That's encapsulation.
Object initializers
var book = new Book { Title = "Noli Me Tángere", Author = "Jose Rizal", Year = 1887 };
Console.WriteLine($"{book.Title} by {book.Author}, {book.Year}");
class Book
{
public string Title { get; set; } = "";
public string Author { get; set; } = "";
public int Year { get; set; }
}
Output
Noli Me Tángere by Jose Rizal, 1887
Static members
var t1 = new Ticket();
var t2 = new Ticket();
var t3 = new Ticket();
Console.WriteLine($"{t1.Number} {t2.Number} {t3.Number}, issued: {Ticket.Issued}");
class Ticket
{
public static int Issued { get; private set; }
public int Number { get; }
public Ticket() => Number = ++Issued;
}
Output
1 2 3, issued: 3
A static member belongs to the class itself, shared by all objects. private set means only the class can change it.
Records
For simple data objects, a record gives you a constructor, properties, ToString and value equality in one line:
var p1 = new Point(3, 4);
var p2 = new Point(3, 4);
Console.WriteLine(p1);
Console.WriteLine(p1 == p2);
record Point(int X, int Y);
Output
Point { X = 3, Y = 4 }
TrueQuick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04