DEV Community

Zahro
Zahro

Posted on

7. Class va Constructor

a) Constructor nima?

Constructor — bu class ning maxsus metodi bo'lib, u yangi obyekt yaratilganda avtomatik ravishda chaqiriladi. Constructor odatda obyektning dastlabki qiymatlarini o'rnatish uchun ishlatiladi.

b) Quyidagi kodni to'ldiring:

class Animal 
{
    public string Name;

    public Animal(string name) 
    {
        Name = name;
    }
}
Enter fullscreen mode Exit fullscreen mode

Animal dog = new Animal("Dog");
Console.WriteLine(dog.Name);

Top comments (0)