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

Objects क्या हैं

एक object किसी class से बनी एक असली चीज़ है, जैसे किसी blueprint से बना एक असली घर। आप एक ही class से कई objects बना सकते हैं, हर एक अपने details के साथ।

In this page:

  1. Objects
Syntax
csharp
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

markup
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})");
    }
}
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. किसी object variable का उपयोग करना जो कभी new से नहीं बनाया गया, जो NullReferenceException फेंकता है।
  2. यह मान लेना कि किसी object को दूसरे variable को assign करना इसे copy कर देता है, जबकि दोनों एक ही object को refer करते हैं।
  3. दो objects को == से compare करना और value equality की उम्मीद करना, जबकि यह default रूप से classes के लिए references compare करता है।
🔒

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.