Coding "This.something"?


(Brandon Anderson) #1

So I’ve seen this. in code a bunch of places. I’ve seen it here is C#, and now I’ve seen it in Java Script for something else. I’ve been trying to figure out what “this.” does. (I feel like I’m in the who’s on first skit). You can’t google it because “this” is ubiquitous to every paragraph of English language. Of course, now that I’m looking for an example, I can’t find one.

So can someone point me to a document that explains what it means when I see this.something ?

And yes I’m aware that it’s probably some super basic code thing that most coders probably already know…


(Aaron Moreng) #2

Refers to the current instance of the class you’re working with.

Edit: Jinx! @josecgomez :slight_smile:


(Jose C Gomez) #3

This refers to the current instance of an object (called internally within a class)

For example if you have a class called Person with a property called name and a method which takes “another” instance of the person class and assigns its name to “this one” you would use this


class Person
{
       public string Name {get;set;}

       public void CopyNameFromPerson(Person p)
       {
                   this.Name = p.Name; //Meaning assign the name property of the person P to my current instance of Person (me, this)
       }
}

@Aaron_Moreng lol


(Brandon Anderson) #4

Thanks, now I have something I can google!


(Chris Conn) #5

Well if it was This.Nothing it would just be null :stuck_out_tongue: