Lesson 3 of 8Article17 min
OOP in C#
C# supports encapsulation through access modifiers and properties. Properties are preferred over public fields for controlled access.
Classes, properties, and methods
C# supports encapsulation through access modifiers and properties. Properties are preferred over public fields for controlled access.
Basic class with property
public class Student
{
public string Name { get; set; }
public int Score { get; set; }
public Student(string name, int score)
{
Name = name;
Score = score;
}
public void PrintSummary()
{
Console.WriteLine($"{Name} scored {Score}");
}
}Inheritance and abstraction basics
- Base classes share common behavior.
- Abstract classes enforce implementation contracts.
- Interfaces define capabilities independent of class hierarchy.