1

Noobie here, but I was wondering why and when would I need to use "this" keyword to access the Promote method in GoldenCustomer when I can already access it since GoldenCustomer is derived from the base class Customer which already has this method? Saw "this" being used in an online course but could't help but wonder.

Edit: No my question isnt a duplicate because the other question doesnt answer when and if it is necessary to use "this" during inheritance.

    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            customer.Promote(); 

            GoldCustomer goldCustomer = new GoldCustomer();
            goldCustomer.OfferVoucher();
        }
    }
    public class GoldCustomer : Customer{
        public void OfferVoucher(){
            this.Promote();   //why is this used here?
        }
    }
    public class Customer{
        public int Id { get; set; }
        public string Name { get; set; }
        public void Promote(){
            int rating = CalculateRating(excludeOrders: true);
            if (rating == 0)
                System.Console.WriteLine("Promoted to level 1");
            else
            System.Console.WriteLine("Promoted to level 2");
        }
        private int CalculateRating(bool excludeOrders){
            return 0;
        }

    }
Mohamed Motaz
  • 391
  • 1
  • 4
  • 13
  • 2
    You are not really have to use `this` unless you have same name for class member and method argument. Than you have to explicitly say `this.name` for member or `name` for argument. I'm not aware of any other scenarios when it required. – Artur Sep 15 '19 at 19:55
  • In your example it does nothing and is exactly the same as the code without it. – iakobski Sep 15 '19 at 20:10
  • @iakobski do u mean in my example specifically or in all cases similar to it? – Mohamed Motaz Sep 15 '19 at 20:12
  • 1
    Have a read of the link above, but I can say that in the whole codebase we have the only use of this is in extension methods or in constructors. Most commonly where there is a name clash between the parameter and the class variable. This is because of adhering to naming standards and can be avoided by using private properties. – iakobski Sep 15 '19 at 21:04
  • _"the other question doesnt answer when and if it is necessary to use "this" during inheritance"_ -- it doesn't address that specifically, because the use of `this` has nothing whatsoever to do with inheritance. Adding the word "inheritance", or any other word that similarly adds zero new context, to the question doesn't change the inherent nature of the question as a duplicate of the other. – Peter Duniho Sep 15 '19 at 23:16
  • @PeterDuniho well that was the point of my question, whether or not "this" has to do with inheritance, and just because my case wasnt mentioned in the other question which u stated mine is a duplicate of, doesnt mean my question isnt a valid one, in fact, it proves my point, I searched everywhere but found no mention of it so I asked here! – Mohamed Motaz Sep 16 '19 at 11:38

1 Answers1

1

The most common uses is when a variable in a method/function has the same name as another class-level variable.

In this case, using the this keyword will tell the compiler that you're referring to the class's variable.

For example:

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

    Public Customer (string Name, string Id)
    {
        this.Name = Name; // "this.Name" is class's Name while "Name" is the function's parameter.
    }
}
 

MSDN Doc for other uses and further reading


Also, a small side-note: ID should always be stored as a string since int has the maximum value of 2147483648, and ID's are treated as a string anyway (you never use math-related functions on it like Id++ or Id = Id * 2 for example).

I'm obviously referring to state-issued IDs like "6480255197" and not "1", "2" and so on.

Michael Yochpaz
  • 187
  • 1
  • 4
  • 15