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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

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

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay