Objects क्या हैं
एक object किसी class से बनी एक असली चीज़ है, जैसे किसी blueprint से बना एक असली घर। आप एक ही class से कई objects बना सकते हैं, हर एक अपने details के साथ।
In this page:
Syntax
ClassName objectName = new ClassName();
objectName.Field = value;
objectName.MethodName();
ClassName other = new ClassName { Field = value }; // object initializer
Objects
एक object किसी class का एक concrete instance है, जो new keyword से बनता है। हर object के पास class के instance fields की अपनी स्वतंत्र copy होती है। आप एक ही class से कई objects बना सकते हैं, हर एक एक अलग entity को represent करते हुए।
Note:
new से बनाए हर object को अपनी memory मिलती है, इसलिए एक object को बदलना कभी दूसरे को प्रभावित नहीं करता।उदाहरण: Objects
using System;
class Point
{
public int X;
public int Y;
}
class Program
{
static void Main(string[] args)
{
Point p1 = new Point { X = 1, Y = 2 };
Point p2 = new Point { X = 5, Y = 9 };
Console.WriteLine($"({p1.X}, {p1.Y})");
Console.WriteLine($"({p2.X}, {p2.Y})");
}
}
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. #}
आम गलतियां
- किसी object variable का उपयोग करना जो कभी
newसे नहीं बनाया गया, जोNullReferenceExceptionफेंकता है। - यह मान लेना कि किसी object को दूसरे variable को assign करना इसे copy कर देता है, जबकि दोनों एक ही object को refer करते हैं।
- दो objects को
==से compare करना और value equality की उम्मीद करना, जबकि यह default रूप से classes के लिए references compare करता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: