Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# this Member Access: Syntax and Use Cases

Learn how the C# this keyword refers to the current instance, resolves member access, enables constructor chaining, and where it cannot be used.

~5 min readC#
#C# this keyword#C# member access#instance members#constructor chaining
C# code editor showing the this keyword used to access a member of the current class instance.

In C#, this is a keyword that refers to the current instance of a class or struct. Inside instance members, you can use it to access fields, properties, methods, and other members of the current object. It is especially useful in constructors and methods where a parameter name would otherwise shadow an instance field. this is not available in static members because those members have no instance context. This article explains the syntax, practical use cases, and limitations of this.

The Role of this in Instance Members

Every instance member of a class or struct has an implicit this reference. When you write this.SomeMember, the compiler resolves SomeMember on the current object. This matters when a parameter or local variable has the same name as an instance field. Consider this common example:

csharp
public class Person { private string name; public void SetName(string name) { this.name = name; } }

Without this.name, the assignment name = name would assign the parameter to itself, leaving the field unmodified. The this keyword disambiguates the member access and ensures the field is updated.

Using this for Constructor Chaining

this can also call another constructor in the same class, a technique called constructor chaining. It reduces code duplication and centralizes initialization logic. The syntax uses this with arguments that match another constructor’s signature:

csharp
public class Product { public string Name { get; } public decimal Price { get; } public Product(string name) : this(name, 0) { } public Product(string name, decimal price) { Name = name; Price = price; } }

The constructor that takes only name delegates to the two-argument constructor, passing a default price. This ensures that any validation or initialization logic in the target constructor runs once. The chained constructor executes before the body of the delegating constructor, which is important for read-only property initialization.

Avoiding Ambiguity with Parameters and Fields

A frequent use of this is in constructors and methods when a parameter name matches an instance field:

csharp
public class Order { private decimal amount; public Order(decimal amount) { this.amount = amount; } }

Without this.amount, the assignment amount = amount would assign the parameter to itself and leave the field unchanged. Writing this.amount makes the target explicit and removes the ambiguity.

this is not required when there is no name conflict. In the following property setter, the backing field _age is unambiguous:

csharp
private int _age; public int Age { get { return _age; } set { _age = value; } }

If you name the backing field age, the setter can still be written as age = value without a compiler error. Some developers write this.age = value to make the field assignment explicit, but that is a style choice, not a requirement. What matters is choosing a convention and applying it consistently.

Passing this as an Argument

this can be passed as an argument to another method, enabling callbacks, builder methods, or dependency-injection registration. For example:

csharp
public class Configuration { public void Register(IServiceCollection services) { services.AddScoped(provider => this); } }

Here, the current Configuration instance is registered with the service container. The receiver can later call public members on that reference.

Extension Methods: The Other this

Extension methods use this as a modifier on the first parameter, not as the instance keyword. That modifier tells the compiler the method can be called with member-access syntax on an instance of the parameter’s type:

csharp
public static class StringExtensions { public static bool IsNullOrEmpty(this string value) { return string.IsNullOrEmpty(value); } }

When you call myString.IsNullOrEmpty(), the compiler passes myString as the value argument. Inside the extension method, you cannot use this to refer to that instance because the method is static; the instance is just a parameter.

Restrictions and Struct Behavior

this is not available in static methods, static constructors, or top-level statements. In each case there is no current instance to refer to. It also cannot be used in a struct’s static members.

In ordinary structs, this is not read-only. You can assign a new struct value to it. For example, a reset method can replace the whole struct:

csharp
public struct Counter { public int Value; public void Reset() { this = default; } }

This is valid for structs but not for classes; in a class, this is read-only. In readonly structs and readonly instance members, this is also read-only, so the same assignment is not allowed. Because structs are value types, assigning to this modifies the copy used to call the method. If the method is called on a property or return value, the original value is not changed.

Inheritance and Member Lookup

When a derived class hides a base member with new, normal member lookup from inside the derived class selects the derived member:

csharp
public class Base { public void Print() { Console.WriteLine("Base"); } } public class Derived : Base { public new void Print() { Console.WriteLine("Derived"); } public void CallPrint() { this.Print(); // calls Derived.Print() ((Base)this).Print(); // calls Base.Print() } }

Member lookup uses the compile-time type of the expression. this is typed as the containing type, so in Derived it selects the derived member; casting to Base changes the lookup target. This is not virtual dispatch.

Style: When this Is Optional

Inside an instance member, this is often optional. The compiler inserts the implicit reference automatically. For example, WidgetName in the following method needs no qualifier:

csharp
public class Widget { public string WidgetName { get; set; } public void Display() { Console.WriteLine(WidgetName); } }

Explicit this is useful when a simple name is ambiguous, such as when a local variable or parameter shadows an instance field. Using this has no runtime cost; it is resolved at compile time. Choose a convention—use this only when necessary, or use it consistently—and apply it consistently across the codebase. Avoid adding this. where it creates noise without improving clarity.

Using nameof With Instance Members

nameof is a compile-time way to get a member name as a string. It is common in argument validation and property-change notifications. Inside an instance member, you can write nameof(Property) without this:

csharp
public string Name { get => _name; set { if (_name != value) { _name = value; OnPropertyChanged(nameof(Name)); } } }

nameof(Name) returns "Name" and stays correct if the property is renamed. It is independent of this; nameof resolves the member name, while this gives you the instance itself. You do not need this for nameof, so the usual form is nameof(Name) rather than nameof(this.Name).

Common Pitfalls

  • Do not use this in a static context; the compiler rejects it.
  • this.Method() uses normal virtual dispatch; it does not force a base implementation. To call a specific base implementation, use base.Method() or cast the reference to the base type.
  • Avoid repeated expensive property access in loops; store the value in a local when the property is not trivial.

Conclusion

this is a core part of C# instance member access. It is required when parameters or locals shadow instance fields, useful for constructor chaining and passing the current object, and unavailable in static contexts. In ordinary structs, this can be assigned, but not in readonly structs or readonly members. Keeping this explicit where it clarifies code—and omitting it where it adds noise—helps produce maintainable C#.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.