Properties

Learn to control access to class fields using properties.

In C#, there are regular and special methods that control access to class fields. These methods are called properties.

Define a property

Properties are created with the following syntax:

// Like other members, properties can have acess modifiers
[access modifiers] property_type property_name
{
    get
    {
        // Get block contains actions to perform when returning the value of the field
    }
    
    set
    {
        // Set block contains actions to perform when setting the value of the field
    }
}

Properties don’t hold any value. They only act as intermediaries between the external code and the fields.

Let’s look at an example.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy