×

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor call. this can also be used to return current class object from a method.  It can also be passed in a method.
C# this keyword Example1:
 using System;
public class Student
{
            int id,batch;
            string name,course;
            public Student(int id,int batch,string name,string course)
            {
                        this.id=id;
                        this.batch=batch;
                        this.name=name;
                        this.course=course;
            }
            public void display()
            {
                        Console.WriteLine(id+" "+batch+" "+name+" "+course);
            }
}
public class ThisExample
{
            public static void Main(string[] args)
            {
            Student s1=new Student(101,2017,"Ayush Sharma","B. Tech.");
            Student s2=new Student(102,2016,"Krishna Kumar","B. Tech.");
            s1.display();
            s2.display();     
            }
}
C# this Keyword Example 2: calling current class method by this
using System;
public class ThisExample2
{
            public void m()
            {
                        Console.WriteLine("Hey You Are in M Method");
            }
            public void n()
            {
                        this.m();//m()
                        Console.WriteLine("Hey you Are in N method");
            }
            public static void Main(string[] args)
            {
                        ThisExample2 t1=new ThisExample2();
                        t1.n();
            }
}
Output
Hey You Are in M Method
Hey you Are in N method
C# this Keyword Example 3: passing this in a method
using System;
public class Employee
{
            public int id,age;
            public string name;
            public float salary;
            public Employee(int id,int age,string name,float salary)
            {
                        this.id=id;
                        this.name=name;
                        this.age=age;
                        this.salary=salary;
            }
            public void display()
            {
                        Display.display(this);
                        Console.WriteLine("printing inside employee class....");
                        Console.WriteLine(id+" "+name+" "+salary+" "+age);
            }
            
            public static void Main(string[] args)
            {
                        Employee emp=new Employee(101,24,"Ravi malik",25000);
                        emp.display();
            }
}
public class Display
{
            public static void display(Employee emp)
            {
                        Console.WriteLine("printing inside Display class…");
                        Console.WriteLine(emp.id+" "+emp.name+" "+emp.salary+" "+emp.age);
            }
}
Output
printing inside Display clas...
101 Ravi malik 25000 24
printing inside employee class....
101 Ravi malik 25000 24
C# this keyword Example: Returning this from the method
using System;
public class Employee
{
            public Employee returnObject()
            {
                        return this;
            }
            public void show()
            {
                        Console.WriteLine("Inside Show Method...");
            }
            public static void Main(string[] args)
            {
                        Employee emp1=new Employee();
                        emp1.show();
                        Employee emp2=emp1.returnObject();
                        emp2.show();
            }
}
Output
Inside Show Method...
Inside Show Method...

Related Topics

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.

C# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.