C# record type

Quyida bir xil vazifani bajaruvchi record va oddiy C# classni ko’ring.

public record OdamRecord(string Ism, string Familya);

public class OdamClass
{
public OdamClass(string ism, string familya)
{
Ism = ism;
Familya= familya;


This content originally appeared on DEV Community and was authored by DEV Community

Quyida bir xil vazifani bajaruvchi record va oddiy C# classni 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
  1. record non-immutable record 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.

Telegram | Instagram


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » C# record type." DEV Community | Sciencx - Wednesday March 9, 2022, https://www.scien.cx/2022/03/09/c-record-type/
HARVARD
DEV Community | Sciencx Wednesday March 9, 2022 » C# record type., viewed ,<https://www.scien.cx/2022/03/09/c-record-type/>
VANCOUVER
DEV Community | Sciencx - » C# record type. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/09/c-record-type/
CHICAGO
" » C# record type." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/09/c-record-type/
IEEE
" » C# record type." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/09/c-record-type/. [Accessed: ]
rf:citation
» C# record type | DEV Community | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.