Permalink
Fetching contributors…
Cannot retrieve contributors at this time
75 lines (61 sloc) 2.55 KB
title description keywords ms.custom ms.date ms.prod ms.technology ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author translation.priority.ht
this (C# Reference)
this keyword (C# Reference)
this (C#), this keyword (C#), this keyword (C# reference), this keyword (C# language reference)
2015-07-20
.net
devlang-csharp
article
this
this_CSharpKeyword
CSharp
this keyword [C#]
d4f827fe-4710-410b-89b8-867dad44b8a3
19
BillWagner
wiwagn
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

this (C# Reference)

The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

[!NOTE] This article discusses the use of this with class instances. For more information about its use in extension methods, see Extension Methods.

The following are common uses of this:

  • To qualify members hidden by similar names, for example:

[!code-cscsrefKeywordsAccess#4]

  • To pass an object as a parameter to other methods, for example:

    CalcTax(this);  
    
  • To declare indexers, for example:

[!code-cscsrefKeywordsAccess#5]

Static member functions, because they exist at the class level and not as part of an object, do not have a this pointer. It is an error to refer to this in a static method.

Example

In this example, this is used to qualify the Employee class members, name and alias, which are hidden by similar names. It is also used to pass an object to the method CalcTax, which belongs to another class.

[!code-cscsrefKeywordsAccess#3]

C# Language Specification

[!INCLUDECSharplangspec]

See Also

C# Reference
C# Programming Guide
C# Keywords
base
Methods