C#-এ this কীওয়ার্ড বর্তমান ক্লাস ইনস্ট্যান্সকে রেফার করতে ব্যবহৃত হয়। এটি মেথড প্যারামিটার এবং ক্লাস ফিল্ডের মধ্যে পার্থক্য করতেও ব্যবহৃত হয় যদি তাদের একই নাম থাকে।
this কীওয়ার্ডের আরেকটি ব্যবহার হলো একই ক্লাসের একটি কনস্ট্রাক্টর থেকে অন্য কনস্ট্রাক্টর কল করা।
এখানে একটি উদাহরণ, যেখানে Student রেকর্ড দেখানো হয়েছে: id, Name, Age, এবং Subject। বর্তমান ক্লাসের ফিল্ডগুলো রেফার করতে this কীওয়ার্ড ব্যবহার করা হয়েছে:
public Student(int id, String name, int age, String subject) {
this.id = id;
this.name = name;
this.subject = subject;
this.age = age;
}
উদাহরণ
C#-এ "this" কীওয়ার্ড ব্যবহার করার সম্পূর্ণ উদাহরণ:
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();
}
}