By 22/07/2015 5 Comments

The this Keyword In C#

  • The this keyword in C# is used inside any method or inside a constructor of the class.
  • When we create an object of a class, we call any method of the class using object reference variable than that object reference is called this.
  • The use of this keyword is an optional but in some situations it is very useful.
  • The this keyword refers as the current object of the current class.
  • The this keyword is very useful inside methods in the class if fields and method parameters have same names, in this situation the parameters or local variables hides these fields. If we use the this keyword with fields it uncovers the instance fields.


using System;
namespace csharpBasic
    class MarkSheet
        // Following is the fields declaration.
        public string StudentName;
        public double AspMarks;

        // Following is a constructor declaration with 2 parameters.
        public MarkSheet(string studentName, double aspMarks)
          /* Note, in the following we declare 
             fields/instance variables with [this] keyword reference.*/
            this.StudentName = studentName;
            this.AspMarks = aspMarks;

        // Following is a string type methods declaration.
        public string DisplayStudentName()
            return this.StudentName;

        // Following is a double type methods declaration.
        public double DisplayObtainAspMarks()
            return this.AspMarks;
    class Program
        // Static main method void type declaration.
        static void Main(string[] args)
            // Following statements create 2 objects of Mark sheet type.
            MarkSheet markSheetOne = new MarkSheet("ABC", 80);
            MarkSheet markSheetTwo = new MarkSheet("XYZ", 70);

            Console.WriteLine("Marksheet 1 information:");

         /* Following statements call methods by each object references
            Note when any specific object reference variable calls 
            a method its reference assigns to the [this] keyword which is described inside that method. */
            Console.WriteLine("Student Name: {0}", markSheetOne.DisplayStudentName());
            Console.WriteLine(" marks: {0}", markSheetOne.DisplayObtainAspMarks());

            Console.WriteLine("Marksheet 2 information:");
            Console.WriteLine("Student Name: {0}", markSheetTwo.DisplayStudentName());
            Console.WriteLine(" marks: {0}", markSheetTwo.DisplayObtainAspMarks());
        The Output will be:
        Marksheet 1 information: 
        Student Name: ABC marks: 80
        Marksheet 2 information:
        Student Name: XYZ marks: 70

Next Tutorial →

← Previous Tutorial

Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .