DEV Community

Sunnat Qayumov
Sunnat Qayumov

Posted on

2 1 2 1 1

Members

Member - Bu struct/class'ning a'zolariga nisbatan qo'llanadigan so'z.

Struct/classning uchta member'i bor:

  • Field
  • Property
  • Method

1. Field: Bu structda oddiy ichida qiymat saqlovchi o'zgaruvchi:

struct Student
{
    public string Name;  // Field
    public int Age;      // Field
}
Enter fullscreen mode Exit fullscreen mode

2. Property: Field'ga qiymat saqlash uchun avvalo Property'dan o'tadi. Property get va set methodlari yordamida qiymatni olib qaytarib beradi:

struct Student
{
    private string name; //private Field

    public string Name //property
    {
        get { return name; } //getter
        set { name = value; } //setter
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Method: Bu ichida biror bir ishni ketma ketligini saqlaydi:

struct Student
{
    public string Name;
    public int Age;

    public void Display()  // Metod
    {
        Console.WriteLine($"Ism: {Name}, Yosh: {Age}");
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Constructor (Ctor): Struct/classdan obyekt ochilganda avtomatik tarzda chaqiriladigan. Biz bilgan usha oddiy method. Lekin ctorning qaytaruvchi qiymati bo'lmaydi va void ham yozilmaydi. Ctor - ctor bo'lishi uchun ochiladigan methodning nomi structning nomi bilan bir-xil bo'lishi shart!!

struct Student
{
    public string Name;
    public int Age;

    public Student(string name, int age) // Konstruktor
    {
        Name = name;
        Age = age;
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Readonly: Struktur Fieldlarini o‘zgartirib bo‘lmaydigan qilib e'lon qilish. Fieldni faqatgina o'qish uchun (ya'ni get uchun) cheklab qo'yadi. Shu holatda unga qiymatni faqatgina ctor'da bera olamiz:

struct Circle
{
    public readonly double Radius; //readonly

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

    public double GetArea() => Math.PI * Radius * Radius;
}
Enter fullscreen mode Exit fullscreen mode

6. Method Overloading: Bu bir xil nomli bo'ladi. Lekin ichidagi parametrlari bilan farq qiladi:

struct Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public double Add(double a, double b)  // Parametrlari farqli
    {
        return a + b;
    }

    public int Add(int a, int b, int c)  // Parametrlari farqli
    {
        return a + b + c;
    }
}
Enter fullscreen mode Exit fullscreen mode

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay