Windows Phone 7 development

Recently, I’ve been looking for all usages of “this” in C#. I couldn’t find any good article, so I’ve “grepped” the spec.
Surprisingly, I had wrong idea of “this” that is the same as in Java.

What is “this”? It is a keyword used in many object-oriented programming languages to refer to the current object. It can be implemented as a pointer (f.e. C++) or reference (f.e. Java), but in C# it has a few different meanings.

Classes

We can use “this” in 3 different context

-call other contructors

-refer to instance fields or methods

-pass current object

public class Person
{
    private string name;
    public Person()
    {
       name = "John";
    }

    public Person(string name)
    : this() // call other constructor
    {
        // this is used to qualify the field
        // “name” is hidden by parameter
        this.name = name;
    }

    public string Name
    {
        get { return name; }
    }

    private void sayHi()
    {
        Console.WriteLine("Hi");
        Foo.SayName(this); //use this to pass current object
    }

    public void Speak()
    {
        this.sayHi(); // use this to refer to an instance method
         Console.WriteLine("Want to come up and see my etchings? ");
    }
}

class Foo
{
public static void SayName(Person person)
{
Console.WriteLine("My name is  {0}", person.Name);
}
}

 

Indexers

Define an indexer for the type to have array-like semantics.

public int this[int index]
{
    get { return array[index]; }
    set { array[index] = value; }
}

Extension methods

class Foo
{
     // with “this” modifiler we can use it like instance method of Person
     public static void SayName(this Person person)
     {
          Console.WriteLine(“My name is  {0}”, person.Name);
     }
}
 

Structures

struct MyVeryOwnInteger
{
    private int x;
    public int Gimme()
    {
    // inside struct this is treated as a variable
    // not reference to current structure
    return this.x;
    }
}
 

From the spe:

1.51.7This access

A this-access consists of the reserved word this.

this-access:
this

A this-access is permitted only in the block of an instance constructor, an instance method, or an instance accessor. It has one of the following meanings:

When this is used in a primary-expression within an instance constructor of a class, it is classified as a value. The type of the value is the instance type (§1.89.1) of the class within which the usage occurs, and the value is a reference to the object being constructed.

When this is used in a primary-expression within an instance method or instance accessor of a class, it is classified as a value. The type of the value is the instance type (§1.89.1) of the class within which the usage occurs, and the value is a reference to the object for which the method or accessor was invoked.

When this is used in a primary-expression within an instance constructor of a struct, it is classified as a variable. The type of the variable is the instance type (§1.89.1) of the struct within which the usage occurs, and the variable represents the struct being constructed. The this variable of an instance constructor of a struct behaves exactly the same as an out parameter of the struct type—in particular, this means that the variable must be definitely assigned in every execution path of the instance constructor.

When this is used in a primary-expression within an instance method or instance accessor of a struct, it is classified as a variable. The type of the variable is the instance type (§1.89.1) of the struct within which the usage occurs.

If the method or accessor is not an iterator (§1.100), the this variable represents the struct for which the method or accessor was invoked, and behaves exactly the same as a ref parameter of the struct type.

If the method or accessor is an iterator, the this variable represents a copy of the struct for which the method or accessor was invoked, and behaves exactly the same as a value parameter of the struct type.

Use of this in a primary-expression in a context other than the ones listed above is a compile-time error. In particular, it is not possible to refer to this in a static method, a static property accessor, or in a variable-initializer of a field declaration.

Yet another example that C# is not Java.

About these ads

Comments on: "Meaning of “this” keyword in C#" (1)

  1. Please update content as soon as possible

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: