×

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# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 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.

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# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute 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# 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# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 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# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 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.

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# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute 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.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.