Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में 'this' कीवर्ड


C# में "this" कीवर्ड का उपयोग क्लास के वर्तमान इंस्टेंस को संदर्भित करने के लिए किया जाता है। इसका उपयोग मेथड पैरामीटर्स और क्लास फील्ड्स के बीच अंतर करने के लिए भी किया जाता है यदि दोनों का नाम समान हो।

"this" कीवर्ड का एक अन्य उपयोग एक कंस्ट्रक्टर से उसी क्लास में दूसरे कंस्ट्रक्टर को कॉल करना है।

यहाँ एक उदाहरण के लिए, हम छात्रों का रिकॉर्ड दिखा रहे हैं यानी: id, Name, Age, और Subject। वर्तमान क्लास के फील्ड्स को संदर्भित करने के लिए, हमने C# में "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;

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();
    }
}

आउटपुट

1 Jack 23 Maths
2 Harry 27 Science
3 Steve 23 Programming
4 David 27 English

this के अन्य उपयोग

  • कंस्ट्रक्टर चेनिंग: this(parameter) से दूसरे कंस्ट्रक्टर को कॉल करें
  • एक्सटेंशन मेथड्स: पहले पैरामीटर पर this लगाकर
  • इंडेक्सर्स: this[int index] प्रॉपर्टी में
  • पास करंट इंस्टेंस: अन्य मेथड्स को this पास करना

कंस्ट्रक्टर चेनिंग उदाहरण

public class Student
{
    public Student() : this(0, "Unknown", 0, "N/A") { }
    
    public Student(int id) : this(id, "Unknown", 0, "N/A") { }
    
    public Student(int id, string name, int age, string subject)
    {
        this.id = id;
        this.name = name;
        this.age = age;
        this.subject = subject;
    }
}
  1. - JavaScript में 'this' कीवर्ड कैसे काम करता है?

    JavaScript में, this कीवर्ड का उपयोग ऑब्जेक्ट या निष्पादित कोड के विषय को संदर्भित करने के लिए एक संदर्भ के रूप में किया जाता है। उदाहरण <script> var college = { subject: Maths, lecturer: Amy, details: function () { document.write(this.subject);

  1. - JavaScript में एरो फंक्शन के अंदर 'this' कीवर्ड कैसे एक्सेस करें?

    एरो फंक्शन में this कीवर्ड जावास्क्रिप्ट का this कीवर्ड उस ऑब्जेक्ट को संदर्भित करता है जिससे यह संबंधित होता है। एरो फंक्शन में, this एक ग्लोबल ऑब्जेक्ट से संबंधित होता है। एक साधारण फंक्शन में, this कीवर्ड undefined परिणाम दे सकता है, लेकिन एरो फंक्शन में यह एक सटीक मान देता है। उदाहरण <html&g

  1. जावास्क्रिप्ट this कीवर्ड समझाएं जावास्क्रिप्ट this कीवर्ड समझाएं

    परिचय जावास्क्रिप्ट this कीवर्ड उस ऑब्जेक्ट को संदर्भित करता है जिससे यह संबंधित होता है। इसका मान इस बात पर निर्भर करता है कि इसे कहाँ और कैसे कॉल किया गया है: अकेले या रेगुलर फ़ंक्शन में: ग्लोबल ऑब्जेक्ट (ब्राउज़र में window, स्ट्रिक्ट मोड में undefined) मेथड में: ऑनर ऑब्जेक्ट इवेंट लिस्टनर में: