Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
so there is "this" is c# which many tutorial online use but i don't understand what it actually does.

p.s explain in layman terms, thank you.

i use it while practicing but don't understand its purpose

What I have tried:

class Circle
{
    float pi = 3.142f;
    int Rad;
    
    public Circle (int Radius)
    {
        this.Rad = Radius;
    }
    public float CaluculateAreaMethod()
    {
        return this.pi * this.Rad * this.Rad;
    }
}
class Program
{
    public static void Main()
    {
        Circle C1 = new Circle(5);
        float Area = C1.CaluculateAreaMethod();

        Console.WriteLine("Area = " + Area);
    }
}
Posted
Updated 17-Jun-18 21:04pm
Comments
[no name] 17-Jun-18 14:41pm    
The equivalent keyword in VB.NET is "Me".

(C# implies: "this" is me).

The this keyword refers to the current instance of the type (e.g. Circle).

However, in this case, its use is superfluous. It is NOT necessary to differentiate the fields (pi and Rad) that it qualifies.

Consider if you had re-named the class-level field Rad as Radius. In that case, within the constructor for the circle, there would be two things named Radius: the parameter to the constructor and the class-level field. So, in that case, the following use of this would be necessary to differentiate between them:

public Circle (int Radius)
{
    this.Radius = Radius;
}
 
Share this answer
 
v3
Eric is absolutely right, but it may help you to think of cars instead of classes.
You are used to the concept of "my car", and "your car"; "this car" and "that car" - and aware that each of them can be different, or change as circumstances do. It you walk down a line of vehicles looking at each in turn, then "this car" changes as you move. The cars don't change, but your reference to them does.

The row of cars is a set a instances of the class "car" , and as you walk, you mentally reassign "this car" to each new instance in turn, so you can discuss each instance using the same words: "what colour is this car?"; "what make is this car?"
The answer changes according to which vehicle you are currently standing in front of because each time you ask, "this car" checks which instance of a car you are referring to and it's specific data.

this in C# serves the same purpose: it refers to the current instance of a class that is "available" when your code is running - and will change as your code outside the class uses different instances.

It's always available - except for static methods which don't work with instances but which deal with matters that affect all instances: "how many wheels has a car?" for example is a static question because you don;t have to walk round the vehicle counting the tires to know that the result is "four" - if it only had two it would be a motorcycle!

Most of the time you don;t need to use it - it's implied by your use of a non-static property or method - the only times you do is when you have a local (or parameter) variable that has the same name as a class level variable and you need to specify the class level version explicitly.

Make sense?
 
Share this answer
 
Comments
Eric Lynch 17-Jun-18 14:32pm    
Nice, I like the analogy. Though, for static classes, what about those funky three wheeled cars :)
OriginalGriff 17-Jun-18 14:40pm    
They are an aberration of nature, and should be classified as (bad) trikes!
Usually, the documentation helps: this (C# Reference) | Microsoft Docs[^].
 
Share this answer
 

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