Share via


How to: Declare and Use Read/Write Properties (C# Programming Guide)

Properties provide the convenience of public data members without the risks that come with unprotected, uncontrolled, and unverified access to an object's data. This is accomplished through accessors: special methods that assign and retrieve values from the underlying data member. The set accessor enables data members to be assigned, and the get accessor retrieves data member values.

This sample shows a Person class that has two properties: Name (string) and Age (int). Both properties provide get and set accessors, so they are considered read/write properties.

Example

class Person
{
    private string name = "N/tcA";
    private int age = 0;

    // Declare a Name property of type string:
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    // Declare an Age property of type int:
    public int Age
    {
        get
        {
            return age;
        }

        set
        {
            age = value;
        }
    }

    public override string ToString()
    {
        return "Name = " + Name + ", Age = " + Age;
    }
}

class TestPerson
{
    static void Main()
    {
        // Create a new Person object:
        Person person = new Person();

        // Print out the name and the age associated with the person:
        Console.WriteLine("Person details - {0}", person);

        // Set some values on the person object:
        person.Name = "Joe";
        person.Age = 99;
        Console.WriteLine("Person details - {0}", person);

        // Increment the Age property:
        person.Age += 1;
        Console.WriteLine("Person details - {0}", person);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
    Person details - Name = N/tcA, Age = 0
    Person details - Name = Joe, Age = 99
    Person details - Name = Joe, Age = 100
*/

Robust Programming

In the previous example, the Name and Age properties are public and include both a get and a set accessor. This allows any object to read and write these properties. It is sometimes desirable, however, to exclude one of the accessors. Omitting the set accessor, for example, makes the property read-only:

public string Name
{
    get
    {
        return name;
    }
}

Alternatively, you can expose one accessor publicly but make the other private or protected. For more information, see Asymmetric Accessor Accessibility.

Once the properties are declared, they can be used as if they were fields of the class. This allows for a very natural syntax when both getting and setting the value of a property, as in the following statements:

person.Name = "Joe";
person.Age = 99;

Note that in a property set method a special value variable is available. This variable contains the value that the user specified, for example:

name = value;

Notice the clean syntax for incrementing the Age property on a Person object:

person.Age += 1;

If separate set and get methods were used to model properties, the equivalent code might look like this:

person.SetAge(person.GetAge() + 1); 

The ToString method is overridden in this example:

public override string ToString()
{
    return "Name = " + Name + ", Age = " + Age;
}

Notice that ToString is not explicitly used in the program. It is invoked by default by the WriteLine calls.

See Also

Reference

Properties (C# Programming Guide)

Classes and Structs (C# Programming Guide)

Concepts

C# Programming Guide