Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
class Rectangle:Shape
{
    double height;
    double width;

    public Rectangle(double height,double width)
    {
       this.height=height;
       this.width=width;
    }
}


class Rectangle:Shape
{
    double rec_Height;
    double rec_Width;

    public Rectangle(double height,double width)
    {
        rec_Height=height;
        rec_Width=width;
    }
}


What I have tried:

My question is why do we use the this keyword?

In my code:

1). I have first initialized the class fields with 'this' keyword.
2). I have again initialized without 'this' keyword.

If I can do it both ways then why do I need to use the 'this' keyword? I mean what is the scenario where 'this' keyword must be used?

Please explain as simply possible as I am quite new to these concepts
Posted
Updated 13-Jul-23 7:44am
Comments
[no name] 13-Jul-23 13:15pm    
"this" is implied in this case. It's use is more obvious if you have "local variables" of the same name; in which case "this" is needed to differentiate the variable from the "class member".
PIEBALDconsult 13-Jul-23 15:17pm    
It's optional there, but I always use it anyway.
Ravi Bhavnani 13-Jul-23 19:58pm    
Me too. It's now part of my team's coding guidelines.

The keyword 'this' tells the compiler that you are dealing with a class attribute and not some other variable of the same name.

It also tells someone else reading your code that a variable being used is a class attribute and not some other variable defined further up in the method. It can make your code clearer and easier to read and understand to a reviewer, someone trying to use your code, or even future you that has to pick up the code at some later date.

Both your examples above are valid. But the first one allows you to use the more natural terms 'height' and 'width' in both the class and constructor. 'This' just makes it clear which is which.
 
Share this answer
 
Comments
Samriddha Chowdhury 13-Jul-23 13:58pm    
@FreedMalloc thank you sir. So it is only explicitly for understanding whether it is a class field right? No technical aspect to understand?
Dave Kreskowiak 13-Jul-23 14:26pm    
Documentation exists for reasons like you're experiencing right now.

this keyword - C# Reference | Microsoft Learn[^]
FreedMalloc 13-Jul-23 14:29pm    
Yes. But also that the field you are using is the one for the current instance of the class.
Graeme_Grant has listed a very good link in Solution 2. I think it explains things far better than I can and also gives other usages for 'this' in C#.
Here is the link to the official documentation for the above usage: this keyword - C# Reference | Microsoft Learn[^]

Quote:
The this keyword refers to the current instance of the class.
 
Share this answer
 
Comments
BillWoodruff 20-Jul-23 3:33am    
+5 current instance is the key concept, here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900