×

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# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute 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# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

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

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

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.

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# 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# 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# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.