Declaring and Using Properties

Like most other members, properties can have access modifiers, can be associated with the type (static) or an instance thereof, and can support other object-oriented modifiers that are discussed in Chapter 14. Their characteristic syntactic surface consists of one or two accessors, which are code blocks that deal with get or set operations:

class Person{    private string _name;    private int _age;    public string Name    {        get { return _name; }        set { _name = value; }    }    public int Age    {        get { return _age; }        set { _age = value; }    }}

In the preceding example, both Name and Age are properties. Each of those properties has a get and a set accessor, both of which are publicly ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.