This content originally appeared on DEV Community and was authored by DEV Community
Quyida bir xil vazifani bajaruvchi record
va oddiy C# class
ni ko'ring.
public record OdamRecord(string Ism, string Familya);
public class OdamClass
{
public OdamClass(string ism, string familya)
{
Ism = ism;
Familya= familya;
}
public string Ism{ get; set; }
public string Familya{ get; set; }
}
var wahidRecord = new PersonRecord("Wahid", "Abduhakimov");
var wahidClass = new PersonClass("Wahid", "Abduhakimov");
1. record
immutable(o'zgarmas)
Yuqoridagi usulda record
yaratilganda uning property'lari immutable*(o'zgartirib bo'lmas)* bo'ladi. Ularga record
yaratilyapkanda qiymat beriladi va keyin o'zgartirishning iloji yo'q. { get; init; }
deganday gap.
var wahid = new OdamRecord("Wahid", "Abduhakimov");
wahid.Ism= "David"; // Compile error beradi
2. Tizimli tenglash (==)
record
structural equality*(tizimli tenlash)* usulidan foydalanadi. Bu degani agar 2 ta record
obyektlarning ichidagi property'lari qiymatlari teng bo'lsa bu ikki obyektlar teng hisoblanadi.
// ... eng tepadagi kodga qarang
var wahid1 = OdamRecord("Wahid", "Abduhakimov");
var wahid2 = OdamRecord("Wahid", "Abduhakimov");
Console.WriteLine($"{wahid1 == wahid2}"); // true
var davron1 = OdamClass("Davron", "Abduhakimov");
var davron2 = OdamClass("Davron", "Abduhakimov");
Console.WriteLine($"{davron1 == davron2}"); // false
-
record
non-immutablerecord
yaratilganda odatda immutable bo'ladi dedik. Lekin uni non-immutable*(o'zgartirsa bo'ladigan)* qilib yaratsa ham bo'ladi.
public record Odam
{
public string Ism { get; set; }
public string Familya{ get; set; }
}
var wahid = new Odam("Wahid, "Abduhakimov");
wahid.Ism = "David"; // muammosiz ishlaydi.
Zeriktirib qo'ymaslik uchun davomi keyingi postlarda 😊
Shu joyigacha erinmay o'qib kelgan ekansiz demak siz yoki C#'ni chindan sevasiz yoki shunchaki meni kuzatuvchimsiz. Ikkala holatda ham vaqtingiz uchun katta rahmat 😍
Yurakcha va yakkashoxlarni bosib, menga "podpiska" tashashni unutmang.
This content originally appeared on DEV Community and was authored by DEV Community
DEV Community | Sciencx (2022-03-09T01:06:20+00:00) C# record type. Retrieved from https://www.scien.cx/2022/03/09/c-record-type/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.