This keyword



Part -21 this

The c# "this" keyword is used to represents to the current instance of the class or a stuct.
this is pointer used to access method parameters,class fields members constructors,instance methods, and instance accessors.

Program 1: Using ‘this’ keyword to refer current class instance members

using System;
 class Add
    {
        public string name;
        public string lname;
        public Add(string name, string lname)
        {
            this.name = name;
            this.lname = lname;
        }
        public void GetName()
        {
            Console.WriteLine(name +"last name is:"+ lname);
        }
    }

It is used to access members from the constructors, instance methods, and instance accessors.

Program 2:


class Add
    {
        public string name;
        public void Addtion(string name)
        {
            this.name = name;
        }
        public void GetName()
        {
            Console.WriteLine(name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Add a = new Add();
            a.Addtion("chandan");
            a.GetName();
            Console.ReadKey();
        }
    }
Program 3: Using ‘this’ keyword to invoke current class method.
class Program
    {
        void display()
        {
            this.show();
            Console.WriteLine("Inside display function");
        }
        void show()
        {
            Console.WriteLine("Inside show funcion");
        }
        static void Main(string[] args)
        {
            Program a = new Program();
            a.display();
            Console.ReadKey();
        }
    }

please comment if you have any doubt  & query regarding to this topic and for another new tutorial topic just drop me a comment.