0
0
CsharpConceptBeginner · 3 min read

What is Property in C#: Definition and Usage

In C#, a property is a member that provides a flexible way to read, write, or compute the value of a private field. It acts like a combination of a variable and methods, allowing controlled access to class data using get and set accessors.
⚙️

How It Works

Think of a property in C# as a smart gatekeeper for a class's data. Instead of letting other parts of your program directly change or see a variable, a property controls how that data is accessed or modified. This is like having a door with a lock that only opens under certain conditions.

Behind the scenes, a property uses two special blocks called get and set. The get block lets you read the value, and the set block lets you change it. This way, you can add rules, like checking if a new value is valid before saving it.

This mechanism helps keep your data safe and your code clean, because you don’t have to write separate methods to get or set values; the property looks like a simple variable from the outside.

💻

Example

This example shows a Person class with a property called Age. It controls access to a private field _age and ensures the age cannot be set to a negative number.

csharp
using System;

class Person
{
    private int _age;

    public int Age
    {
        get { return _age; }
        set
        {
            if (value < 0)
            {
                Console.WriteLine("Age cannot be negative.");
            }
            else
            {
                _age = value;
            }
        }
    }
}

class Program
{
    static void Main()
    {
        Person person = new Person();
        person.Age = 25;
        Console.WriteLine($"Age: {person.Age}");

        person.Age = -5; // Invalid
        Console.WriteLine($"Age after invalid set: {person.Age}");
    }
}
Output
Age: 25 Age cannot be negative. Age after invalid set: 25
🎯

When to Use

Use properties when you want to control how data inside a class is accessed or changed without exposing the data directly. This is useful when you need to validate input, log changes, or compute values on the fly.

For example, in a banking app, you might use a property to ensure an account balance never goes below zero. Or in a game, a property could update the player's score and also update the display automatically.

Properties make your code safer and easier to maintain by hiding the internal details and providing a clear interface.

Key Points

  • Properties combine the simplicity of variables with the power of methods.
  • They use get and set accessors to control reading and writing.
  • Properties help protect data by adding validation or logic.
  • They improve code readability and maintainability.

Key Takeaways

A property in C# controls access to class data using get and set blocks.
Properties allow validation and logic when reading or writing values.
They help keep data safe and code clean by hiding internal fields.
Use properties to provide a simple and controlled interface to data.