Skip to main content

10. Properties in C#

Level: Beginner

ℹ️ What You'll Learn
  • What a property is
  • Why properties are better than public fields
  • Use get and set
  • 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:

CodeMeaning
getOther code can read the value
setOther 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

💻 Try It — Console App
💡 Paste into Program.cs and press F5⌥ GitHub
public class Student
{
private double _percentage;

public string Name { get; set; } = "";
public int RollNumber { get; private set; }

public double Percentage
{
get { return _percentage; }
set
{
if (value < 0 || value > 100)
{
Console.WriteLine("Invalid percentage.");
return;
}

_percentage = value;
}
}

public Student(int rollNumber)
{
RollNumber = rollNumber;
}

public void PrintInfo()
{
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Roll Number: {RollNumber}");
Console.WriteLine($"Percentage: {Percentage}");
}
}

Student student = new Student(101);
student.Name = "Sahasra Kumar";
student.Percentage = 84.8;
student.PrintInfo();

student.Percentage = -20;

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:

  • Name
  • RollNumber
  • ClassName
  • Percentage

FeeAccount:

  • TotalFees
  • PaidAmount
  • Balance

Teacher:

  • Name
  • EmployeeId
  • Subject
  • Salary

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:

  1. Change percentage to 95
  2. Change percentage to 120
  3. Try to change RollNumber after creating the object
  4. Add a new property called ClassName

ℹ️ Video Tutorial

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:

  • Name
  • EmployeeId with private setter
  • Subject
  • ExperienceYears with validation: cannot be negative

Create one teacher object and print the details.

Quick Revision

QuestionAnswer
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

🎯 Q1: What is a property in C#?

A property is a controlled way to read and write data inside a class.

🎯 Q2: What is the difference between field and property?

A field is direct storage. A property can control access and add validation.

🎯 Q3: What does private set mean?

private set means outside code can read the value, but only the class can change it.


🤖Use AI to Learn Faster
⚠️ Important for beginners: Do NOT use AI to write your code yet. Type every example yourself. Your brain learns by doing, not by reading AI output. Use AI only to explain and quiz you — not to code for you. Once you have strong fundamentals, AI becomes a powerful productivity tool for repetitive tasks.

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.

Next Article

Encapsulation and Access Modifiers ->

nexcoding.in