Accessing Fields

Depending on whether the field is declared as static or as an instance field, it can be accessed in a different manner. For instance fields, you must qualify the lookup by specifying the target instance. To access a field from within the declaring type, you can omit the target instance qualification or use this:

class Person{    private string _name;    private int _age;    public override string ToString()    {        return _name + " is " + this._age;    }}

You’ll learn what the override modifier on a method means in Chapter 14. It’s also essential to realize that the private access modifier doesn’t prevent you from accessing another instance’s private fields as long as that access happens within ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.