C# Struct

Struct(Struktura) – bu C# dasturlash tilida qiymat tipidagi (value type) ma’lumot tuzilmasi bo‘lib, obyekt yaratish va bir nechta o‘zgaruvchini guruhlash uchun ishlatiladi.
Struct classga o‘xshaydi, lekin u qiymat (value type) sifatida ishlaydi va tezr…


This content originally appeared on DEV Community and was authored by Alisher Maxamadjonov

Struct(Struktura) – bu C# dasturlash tilida qiymat tipidagi (value type) ma’lumot tuzilmasi bo‘lib, obyekt yaratish va bir nechta o‘zgaruvchini guruhlash uchun ishlatiladi.
Struct classga o‘xshaydi, lekin u qiymat (value type) sifatida ishlaydi va tezroq ishlaydi.
Struct maydonlar (field), property’lar, konstruktorlar, metodlarni qo‘llab-quvvatlaydi.

Oddiy Structga misol

struct Point
{
    public int X;
    public int Y;

    public void ShowPoint()
    {
        Console.WriteLine($"Nuqta koordinatalari: ({X}, {Y})");
    }
}

class Program
{
    static void Main()
    {
        Point p;  // Struct yaratish
        p.X = 10;
        p.Y = 20;
        p.ShowPoint();  // Natija: Nuqta koordinatalari: (10, 20)
    }
}

Struct Konstructor bilan

struct Rectangle
{
    public double Width;
    public double Height;

    public Rectangle(double width, double height)  // Konstruktor
    {
        Width = width;
        Height = height;
    }

    public double GetArea() => Width * Height;  // Yuzani hisoblaydi

    public double GetPerimeter() => 2 * (Width + Height);  // Perimetrni hisoblaydi
}

class Program
{
    static void Main()
    {
        Rectangle rect = new Rectangle(5, 3);
        Console.WriteLine($"Yuza: {rect.GetArea()}"); // 15
        Console.WriteLine($"Perimetr: {rect.GetPerimeter()}"); // 16
    }
}

Struct cheklovlari:
Meros ola olmaydi (inheritance yo‘q).
Null bo‘la olmaydi (null qabul qilmaydi).
Default konstruktor (parametrsiz) yaratib bo‘lmaydi.

Struct – qiymat turi (value type) bo‘lib, tezroq ishlaydi va heap emas, stack xotirada saqlanadi.
Struct kichik va oddiy obyektlar uchun ishlatiladi.
class bilan solishtirganda tezroq ishlaydi, lekin kamroq imkoniyatlarga ega.


This content originally appeared on DEV Community and was authored by Alisher Maxamadjonov


Print Share Comment Cite Upload Translate Updates
APA

Alisher Maxamadjonov | Sciencx (2025-03-17T15:01:38+00:00) C# Struct. Retrieved from https://www.scien.cx/2025/03/17/c-struct/

MLA
" » C# Struct." Alisher Maxamadjonov | Sciencx - Monday March 17, 2025, https://www.scien.cx/2025/03/17/c-struct/
HARVARD
Alisher Maxamadjonov | Sciencx Monday March 17, 2025 » C# Struct., viewed ,<https://www.scien.cx/2025/03/17/c-struct/>
VANCOUVER
Alisher Maxamadjonov | Sciencx - » C# Struct. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/17/c-struct/
CHICAGO
" » C# Struct." Alisher Maxamadjonov | Sciencx - Accessed . https://www.scien.cx/2025/03/17/c-struct/
IEEE
" » C# Struct." Alisher Maxamadjonov | Sciencx [Online]. Available: https://www.scien.cx/2025/03/17/c-struct/. [Accessed: ]
rf:citation
» C# Struct | Alisher Maxamadjonov | Sciencx | https://www.scien.cx/2025/03/17/c-struct/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.