Learn How To Program In C# Part 26 – This Keyword


Hey, everyone. In this video, we’ll be talking about this
reserved keyword, which among many other things helps us to eliminate naming conflicts. From our previous example, we have argFirstname
and argLastname. class Person
{ public string Firstname { get; set; }
public string Lastname { get; set; } public Person(string argFirstname, string
argLastname) {
Firstname=argFirstname; Lastname=argLastname;
} public static string FormatFullname()
{ return $”{Firstname} – {Lastname}”;
} }
The reason I did that is so that I can avoid a conflict because we already have two properties
which are similarly named as the constructor parameters. So what I can do is I can use the this reserved
keyword and just take out these args from the front since it’s more appealing this
way and more readable. And then I can say this.Firstname and this.Lastname. class Person
{ public string Firstname { get; set; }
public string Lastname { get; set; } public Person(string Firstname, string Lastname)
{ this.Firstname=argFirstname;
this.Lastname=argLastname; } public static string FormatFullname()
{ return $”{Firstname} – {Lastname}”;
} }
this reserved keyword is a part of an instance expression and cannot refer to a static member. Remember how I said that the static members
belong to class and do not belong to the object instance? That’s what I mean. this reserved keyword cannot be inside of
a static class, and it cannot refer to a static member. So it only has to do with non-static stuff. In this case, our Firstname and Lastname are
non-static, our constructor is non-static, and our class is non-static. That’s why we can instantiate it. I just wanted to show you how to use the this
reserved keyword and when to use it and show you the benefits of it. That’s it, pretty short video. If you like it, please give it a thumbs up. If you haven’t subscribed, please do so. I will definitely appreciate. And if you have any questions about this video
or any other ones that I have made, please leave them below, and I will get back to you
as soon as possible. Thank you for watching.

13 Replies to “Learn How To Program In C# Part 26 – This Keyword”

  1. I see your channel is new but it has a lot of potential. Keep up the good work and great quality and you'll become a celebrity in your own right. And remember, consumers (or in this case subscribers) only come for one thing, content! So keep at it and take some tips from other experienced channels like +thenewboston. Good luck!

  2. my teacher said by using 'this' keyword you can call a constructor within a constructor , does it make any sense?

  3. It is also used to pass an object to the method CalcTax, which belongs to another class.
    plzz explain this stuff here's the link.
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this

  4. what are possible applications of this other than just to have different variable names in your constructor parameters?

Leave a Reply

Your email address will not be published. Required fields are marked *