this Keyword क्या है
this keyword का मतलब है 'मैं, वह object जिसके अंदर मैं अभी हूं'। यह अपनी चीज़ों की ओर इशारा करते समय 'मेरा' कहने जैसा है, किसी और की तरफ नहीं।
In this page:
Syntax
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- किसी static method के अंदर
thisउपयोग करना, जहां यह उपलब्ध नहीं है। - किसी parameter का नाम किसी field जैसा रखना और assign करते समय
this.भूल जाना, जिससे parameter खुद को assign हो जाता है। - हर जगह अनावश्यक रूप से
thisउपयोग करना, code को गड़बड़ करते हुए।
चैप्टर सारांश
- Classes types define करती हैं और objects इनके instances हैं, constructors से बने।
- Properties और access modifiers नियंत्रित करते हैं कि data कैसे उजागर हो।
- Static members class से belong करते हैं, और
thiscurrent object को refer करता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: