
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
‘this’ keyword in C#
The “this” keyword in C# is used to refer to the current instance of the class. It is also used to differentiate between the method parameters and class fields if they both have the same name.
Another usage of “this” keyword is to call another constructor from a constructor in the same class.
Here, for an example, we are showing a record of Students i.e: id, Name, Age, and Subject. To refer to the fields of the current class, we have used the “this” keyword in C# −
public Student(int id, String name, int age, String subject) { this.id = id; this.name = name; this.subject = subject; this.age = age; }
Example
Let us see the complete example to learn how to work with the “this” keyword in C# −
using System.IO; using System; class Student { public int id, age; public String name, subject; public Student(int id, String name, int age, String subject) { this.id = id; this.name = name; this.subject = subject; this.age = age; } public void showInfo() { Console.WriteLine(id + " " + name+" "+age+ " "+subject); } } class StudentDetails { public static void Main(string[] args) { Student std1 = new Student(001, "Jack", 23, "Maths"); Student std2 = new Student(002, "Harry", 27, "Science"); Student std3 = new Student(003, "Steve", 23, "Programming"); Student std4 = new Student(004, "David", 27, "English"); std1.showInfo(); std2.showInfo(); std3.showInfo(); std4.showInfo(); } }
- Related Articles
- This keyword in Java
- This keyword in Dart Programming
- Explain JavaScript "this" keyword?
- How does the “this” keyword work in JavaScript?
- How to work with this keyword in Java?
- What are the uses of this keyword in java?
- Can we call methods using this keyword in java?
- What is the use of this keyword in TypeScript?
- Can we return this keyword from a method in java?
- C++ Program to Show Use of This Keyword in Class
- Can we use "this" keyword in a static method in java?
- When should I use the keyword ‘this’ in a Java class?
- What are the 6 ways to use this keyword in Java?
- Is it possible to use this keyword in static context in java?
- What are all the ways keyword ‘this’ can be used in Java?

Advertisements