DEV Community

umida5
umida5

Posted on

Class va Constructor

C# tilida konstruktor (constructor) — bu obyekt yaratishda avtomatik ravishda chaqiriladigan maxsus metoddir. Konstruktor obyektning boshlang'ich qiymatlarini o'rnatish uchun ishlatiladi.

Konstruktorning ba'zi asosiy xususiyatlari:

  1. Nomi: Konstruktorning nomi sinf (class) nomi bilan bir xil bo'lishi kerak.
  2. Qaytish tipi: Konstruktor hech qanday qaytish tipiga ega emas (return type yo'q).
  3. Avtomatik chaqirilishi: Obyekt yaratilganda avtomatik ravishda chaqiriladi.
public class Car
{
    public string Model { get; set; }
    public int Year { get; set; }

    // Konstruktor
    public Car(string model, int year)
    {
        Model = model;
        Year = year;
    }
}

// Obyekt yaratish
Car myCar = new Car("Toyota", 2020);
Console.WriteLine($"Model: {myCar.Model}, Year: {myCar.Year}");

Enter fullscreen mode Exit fullscreen mode

Bu misolda Car sinfida konstruktor mavjud bo'lib, u model va year parametrlarini qabul qiladi. Obyekt yaratish jarayonida bu qiymatlar konstruktor orqali o'rnatiladi.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs