BCA / B.Tech 9 min read

Structs in C# .NET in Hindi

Structs in C# .NET in Hindi 


Structs (structs का पूरा नाम structure है) C# में value type होती है। Structs का उपयोग तब किया जाता है जब हमें छोटे और हल्के-weight data types चाहिए जो value को directly memory में store करें।

Structs को हम classes की तरह ही use करते हैं, लेकिन उनके बीच कुछ मुख्य अंतर होते हैं:

  • Structs value type होती हैं, जबकि classes reference type होती हैं।
  • Structs को heap memory में नहीं बल्कि stack memory में store किया जाता है।
  • Structs में inheritance support नहीं होता (आप किसी struct को दूसरी struct से derive नहीं कर सकते)।
  • Structs का उपयोग (Using Structs)
Structs को Declare और Use कैसे करें:

using System;

struct Point
{
    public int X;
    public int Y;

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public void Display()
    {
        Console.WriteLine($"Point is at ({X}, {Y})");
    }
}

class Program
{
    static void Main()
    {
        Point p = new Point(10, 20); // Struct का object create किया
        p.Display(); // Output: Point is at (10, 20)
    }
}
इस example में, हमने Point नाम का एक struct बनाया है जिसमें दो variables (X और Y) और एक method (Display) है। इसे object की तरह use किया जा सकता है।

Overloading Structs : Overloading का मतलब है एक struct में multiple constructors या methods define करना, जिनके parameters अलग-अलग होते हैं।

struct Rectangle
{
    public int Length;
    public int Breadth;

    // Constructor Overloading
    public Rectangle(int side)
    {
        Length = Breadth = side; // Square बन रहा है
    }

    public Rectangle(int length, int breadth)
    {
        Length = length;
        Breadth = breadth;
    }

    public int Area()
    {
        return Length * Breadth;
    }
}

class Program
{
    static void Main()
    {
        Rectangle square = new Rectangle(5); // Overloaded constructor (Square)
        Console.WriteLine("Square Area: " + square.Area());

        Rectangle rect = new Rectangle(4, 6); // Overloaded constructor (Rectangle)
        Console.WriteLine("Rectangle Area: " + rect.Area());
    }
}
यहां struct में दो अलग-अलग constructors दिए गए हैं, जिनसे आप square और rectangle बना सकते हैं।

Structs को Call करना (Calling Structs in .NET with C#) : Structs को call करना class objects की तरह ही होता है, लेकिन struct को directly stack पर store किया जाता है। नीचे एक example है:

struct Circle
{
    public double Radius;

    public Circle(double radius)
    {
        Radius = radius;
    }

    public double Area()
    {
        return Math.PI * Radius * Radius;
    }
}

class Program
{
    static void Main()
    {
        Circle c = new Circle(5); // Struct instance create किया
        Console.WriteLine("Circle Area: " + c.Area());
    }
}


Structs का इस्तेमाल कब करें?

  • जब data lightweight हो (जैसे coordinates, colors, etc.)।
  • जब data short-lived हो और stack memory में store किया जा सके।
  • जब inheritance की जरूरत न हो।
  • Structs का सही उपयोग करने से performance में improvement हो सकता है क्योंकि stack memory पर access faster होती है।

In this Chapter

Structs in C# .NET in Hindi
Introduction of .Net in Hindi
Web Services in Hindi | वेब सर्विसेस हिंदी में
WSDL in Hindi | WSDL हिंदी में
Boxing & Unboxing in ADO.NET in Hindi
CLR in Hindi | CLR क्या है?
Common Types System in Hindi
MSIL in Hindi
Assemblies & Class Libraries in Hindi
Project of .Net in Hindi
What is VB.NET and IDE in Hindi | वीबी.नेट क्या है ?
Intermediate Language in Hindi
Object Orientation in Hindi
Managed Execution in Hindi
Rapid Development in Hindi
Windows Presentation Foundation in Hindi
Whats new For .NET framework 3.5?
Windows Workflow Foundation (WWF) in Hindi
Windows Card Space in Hindi
Windows Communication Foundation in Hindi
How To Install and Use The Visual Studio 2008
How to Working With Visual Studio 2008
Types of Visual Studio 2008 in Hindi
Visual Studio 2008 IDE in Hindi
How To Create Console Application in Hindi
Introduction of C# in .NET in Hindi
Classes of .NET With C# in Hindi
Properties of .NET With C# in Hindi
Delegates & Events in Hindi
Generic Collections in .NET (C#) in Hindi
Type Safety in Hindi
Nullable Types in .NET in Hindi
ADO.NET in Hindi | ADO.NET क्या है?
SQL Connection Object in Hindi
SQL Command in Hindi
LINQ in Hindi | LINQ क्या है?
What is Using Stored Procedures?
Windows Application in .NET in Hindi | Windows Application क्या है?
BCA || .NET with C# 2023 Paper | MDSU Exam Paper
.NET with C# All Important Questions and Answers in Hindi (MDSU)
BCA || .NET with C# 2025 Paper | MDSU Exam Paper