Skip to Content
Learning C# 3.0
book

Learning C# 3.0

by Jesse Liberty, Brian MacDonald
November 2008
Beginner
696 pages
17h 43m
English
O'Reilly Media, Inc.
Content preview from Learning C# 3.0

The this Keyword

The keyword this refers to the current instance of an object. The this reference is a hidden parameter in every nonstatic method of a class (we’ll discuss static methods shortly). There are three ways in which the this reference is typically used. The first way is to qualify instance members that have the same name as parameters, as in the following:

public void SomeMethod (int length)
{
   this.length = length;
}

In this example, SomeMethod( ) takes a parameter (length) with the same name as a member variable of the class. The this reference is used to resolve the ambiguity. Whereas this.length refers to the member variable, length refers to the parameter.

You can, for example, use the this reference to make assigning to a field more explicit:

public void SetBox(int length, int newWidth, int newHeight)
{
   this.length = length;       // use of "this" required
   this.width = newWidth;      // use of "this" optional
   height = newHeight;         // use of "this" not needed

If the name of the parameter is the same as the name of the member variable, you must use the this reference to distinguish between the two, but if the names are different (such as newWidth and newHeight), the use of the this reference is optional.

Tip

The argument in favor of naming the argument to a method that is the same as the name of the member is that the relationship between the two is made explicit. The counterargument is that using the same name for both the parameter and the member variable can cause confusion as to which ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C# 3.0 Design Patterns

C# 3.0 Design Patterns

Judith Bishop
Windows Runtime via C#

Windows Runtime via C#

Jeffrey Richter and Maarten van de Bospoort
C# 7.0 in a Nutshell

C# 7.0 in a Nutshell

Joseph Albahari, Ben Albahari

Publisher Resources

ISBN: 9780596155018Errata Page