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 code defined on the type. To ...

Get C# 4.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.