← Back to C# Course | Chapter 6: OOP Basics | Lesson 7 of 7

this Keyword क्या है

this keyword का मतलब है 'मैं, वह object जिसके अंदर मैं अभी हूं'। यह अपनी चीज़ों की ओर इशारा करते समय 'मेरा' कहने जैसा है, किसी और की तरफ नहीं।

In this page:

  1. The this Keyword
Syntax
csharp
class ClassName
{
    public type Field;

    public ClassName(type field)
    {
        this.Field = field;   // this = the current object
    }
}

The this Keyword

this किसी non-static method या constructor के अंदर current object instance को refer करता है। यह सबसे ज़्यादा किसी instance field को एक जैसे नाम वाले parameter से अलग करने के लिए उपयोग होता है। this का उपयोग उसी class पर दूसरे constructor को call करने के लिए भी किया जा सकता है।

Note: किसी constructor में this.fieldName = fieldName; उपयोग करें जब parameter field के नाम से मेल खाए।

उदाहरण: The this Keyword

markup
using System;

class Rectangle
{
    public int Width;
    public int Height;

    public Rectangle(int width, int height)
    {
        this.Width = width;
        this.Height = height;
    }

    public int Area()
    {
        return this.Width * this.Height;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle(4, 5);
        Console.WriteLine(r.Area());
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी static method के अंदर this उपयोग करना, जहां यह उपलब्ध नहीं है।
  2. किसी parameter का नाम किसी field जैसा रखना और assign करते समय this. भूल जाना, जिससे parameter खुद को assign हो जाता है।
  3. हर जगह अनावश्यक रूप से this उपयोग करना, code को गड़बड़ करते हुए।
चैप्टर सारांश
  • Classes types define करती हैं और objects इनके instances हैं, constructors से बने।
  • Properties और access modifiers नियंत्रित करते हैं कि data कैसे उजागर हो।
  • Static members class से belong करते हैं, और this current object को refer करता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.