13.4. Object Self-Referencing with "this"

In client code, such as the Main method of a program, we declare reference variables with which to store references to objects:

Student s = new Student(); // a reference variable of type Student

and can then conveniently access the objects that these reference variables refer to by manipulating the reference variables themselves:

s.Name = "Fred";

When we're executing the code that comprises the body of one of an object's own methods, we sometimes need the object to be able to refer to itself—that is, to self-reference—as in this next bit of code:

public class Student
{
  Professor facultyAdvisor;
  // other details omitted
public void SelectAdvisor(Professor p) {
					// We're down in the "bowels" of the SelectAdvisor() ...

Get Beginning C# 2008 Objects: From Concept to Code 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.