10. Properties in C#
Level: Beginner
- What a property is
- Why properties are better than public fields
- Use
getandset - Create auto-properties
- Use private setters for safer data
- Add simple validation in a property
- Use properties in the School Management System
In the previous article, you learned classes and objects.
Now we learn how a class stores data safely.
In C#, a property is the normal way to read and write data inside a class.
The Problem: Public Fields Can Be Changed Wrongly
Imagine this class:
public class Student
{
public string Name;
public int Percentage;
}
This looks easy, but it is unsafe.
Student student = new Student();
student.Name = "";
student.Percentage = -50;
Problem:
Name should not be empty.
Percentage should not be negative.
But public fields allow both mistakes.
Properties help us control this.
Quick Definitions
- Field - Variable inside a class
- Property - Controlled way to read or write class data
- get - Reads the value
- set - Changes the value
- Auto-property - Short property syntax used for simple data
- Private setter - Allows reading from outside, but changing only inside the class
- Validation - Checking data before saving it
Step 1: Auto-Properties
Auto-properties are the simplest properties.
public class Student
{
public string Name { get; set; } = "";
public int RollNumber { get; set; }
public double Percentage { get; set; }
}
Use them like this:
Student student = new Student();
student.Name = "Sahasra Kumar";
student.RollNumber = 101;
student.Percentage = 84.8;
Meaning:
| Code | Meaning |
|---|---|
get | Other code can read the value |
set | Other code can change the value |
= "" | Starting value for text |
Step 2: Read-Only from Outside
Sometimes outside code should read a value, but not change it directly.
Example: roll number should be set only when creating the student.
public class Student
{
public string Name { get; set; } = "";
public int RollNumber { get; private set; }
public Student(int rollNumber)
{
RollNumber = rollNumber;
}
}
Usage:
Student student = new Student(101);
student.Name = "Priya";
Console.WriteLine(student.RollNumber);
This is not allowed:
student.RollNumber = 999; // Error: setter is private
Step 3: Property with Validation
Use a full property when you need to check the value.
public class Student
{
private double _percentage;
public double Percentage
{
get { return _percentage; }
set
{
if (value < 0 || value > 100)
{
Console.WriteLine("Percentage must be between 0 and 100.");
return;
}
_percentage = value;
}
}
}
Here:
_percentage stores the real value.
Percentage controls access to that value.
Full Example: Student Properties
Expected output:
Name: Sahasra Kumar
Roll Number: 101
Percentage: 84.8
Invalid percentage.
When You'll Use This in SMS
Properties are used in almost every School Management class.
Student:
NameRollNumberClassNamePercentage
FeeAccount:
TotalFeesPaidAmountBalance
Teacher:
NameEmployeeIdSubjectSalary
Real rule:
Use properties for class data.
Use validation when wrong data can damage the system.
Try This Now
Run the full example. Then experiment:
- Change percentage to
95 - Change percentage to
120 - Try to change
RollNumberafter creating the object - Add a new property called
ClassName
C# properties video coming soon on NexCoding YouTube. Subscribe for updates.
Common Mistakes
Mistake 1: Using public fields everywhere
Public fields give no control. Prefer properties.
Mistake 2: Forgetting starting values for strings
Use = "" for beginner examples to avoid null confusion.
Mistake 3: Using validation for every property
Use validation only when the value needs rules.
Practice Task
Create a Teacher class with:
NameEmployeeIdwith private setterSubjectExperienceYearswith validation: cannot be negative
Create one teacher object and print the details.
Quick Revision
| Question | Answer |
|---|---|
| What is a property? | Controlled way to read/write class data |
What does get do? | Reads a value |
What does set do? | Changes a value |
| What is an auto-property? | Short property syntax |
| Why use private set? | To stop outside code from changing the value |
A property is a controlled way to read and write data inside a class.
A field is direct storage. A property can control access and add validation.
private set means outside code can read the value, but only the class can change it.
Use ChatGPT, Claude, or Copilot to go deeper on C# properties. Try these prompts:
"Explain C# properties like I am a beginner""Give me 5 practice tasks for get and set""Explain property validation using school examples""Quiz me with 5 questions about C# properties"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.