简体   繁体   English

C#(ASP.NET Core MVC)中的记录类型有什么用?

[英]What's the use of the record type in C# (in ASP.NET Core MVC)?

Recently I discovered a third type of object-type in C#: the record ;最近我在 C# 中发现了第三种对象类型: record actually 2: the record struct and the record class .实际上 2: record structrecord class As I understand the type does not differ significantly from normal classes (I never used structs in my programs).据我了解,该类型与普通类没有显着差异(我从未在我的程序中使用过结构)。 The big advantage seems to me that they are easier to construct, because of their constructor+property-in-1 syntax.在我看来,最大的优势是它们更容易构造,因为它们的构造函数+property-in-1 语法。 Is this a correct point of view?这是一个正确的观点吗? I would like to use them then for building my View Models in the ASP.NET Core MVC framework.然后我想用它们在 ASP.NET Core MVC 框架中构建我的视图模型。 I don't know why they just added the syntax to classes?我不知道他们为什么只是将语法添加到类中? The doubt that I miss something holds me back from using them.对我错过某些东西的怀疑使我无法使用它们。

Below are some advantages scenarios where you can leverage the use of record type, check the performance part:)以下是一些可以利用记录类型的优势场景,请检查性能部分:)

- Concise syntax for creating a reference type with immutable properties - 用于创建具有不可变属性的引用类型的简洁语法

You can use positional parameters to declare properties of a record and to initialize the property values when you create an instance您可以使用位置参数来声明记录的属性并在创建实例时初始化属性值

public record Person(string FirstName, string LastName);

public static void Main()
{
    Person person = new("Nancy", "Davolio");
    Console.WriteLine(person);
    // output: Person { FirstName = Nancy, LastName = Davolio }
}
  • Built-in behavior useful for a data-centric reference type:对以数据为中心的引用类型有用的内置行为:

    - Value equality - 价值平等

    For class types, two objects are equal if they refer to the same object in memory while For record types, including record struct and readonly record struct, two objects are equal if they are of the same type and store the same values.对于 class 类型,如果两个对象在 memory 中引用相同的 object,则两个对象相等,而对于记录类型,包括记录结构和只读记录类型结构,两个对象相等,如果它们是

    - Concise syntax for nondestructive mutation - 无损突变的简洁语法

    If you need to copy an instance with some modifications, you can use a with expression to achieve nondestructive mutation.如果你需要复制一个经过一些修改的实例,你可以使用 with 表达式来实现无损突变。 A with expression makes a new record instance that is a copy of an existing record instance, with specified properties and fields modified. with 表达式创建一个新记录实例,它是现有记录实例的副本,并修改了指定的属性和字段。

     public record Person(string FirstName, string LastName) { public string[] PhoneNumbers { get; init; } } public static void Main() { Person person1 = new("Nancy", "Davolio") { PhoneNumbers = new string[1] }; Person person2 = person1 with { FirstName = "John" }; Console.WriteLine(person2); // output: Person { FirstName = John, LastName = Davolio, PhoneNumbers = System.String[] } Console.WriteLine(person1 == person2); // output: False. . . person2 = person1 with { }; Console.WriteLine(person1 == person2); // output: True

    - Built-in formatting for display - 用于显示的内置格式

    Record types have a compiler-generated ToString method that displays the names and values of public properties and fields.记录类型具有编译器生成的 ToString 方法,该方法显示公共属性和字段的名称和值。 The ToString method returns a string of the following format: ToString 方法返回以下格式的字符串:

     "<record type name> { <property name> = <value>, <property name> = <value>, ...}"
  • Support for inheritance hierarchies支持 inheritance 层次结构

    A record can inherit from another record.一条记录可以继承自另一条记录。 However, a record can't inherit from a class, and a class can't inherit from a record.但是,记录不能从 class 继承,并且 class 不能从记录继承。

  • Positional parameters in derived record types派生记录类型中的位置参数

    The derived record declares positional parameters for all the parameters in the base record primary constructor.派生记录为基本记录主构造函数中的所有参数声明位置参数。 The base record declares and initializes those properties.基本记录声明并初始化这些属性。 The derived record doesn't hide them, but only creates and initializes properties for parameters that aren't declared in its base record.派生记录不会隐藏它们,而只会为未在其基本记录中声明的参数创建和初始化属性。

  • Equality in inheritance hierarchies inheritance 层次结构中的平等

    For two record variables to be equal, the run-time type must be equal.要使两个记录变量相等,运行时类型必须相等。

  • with expressions in derived records在派生记录中使用表达式

    The result of a with expression has the same run-time type as the expression's operand. with 表达式的结果与表达式的操作数具有相同的运行时类型。 All properties of the run-time type get copied, but you can only set properties of the compile-time type运行时类型的所有属性都被复制,但您只能设置编译时类型的属性

  • PrintMembers formatting in derived records派生记录中的 PrintMembers 格式

    The synthesized PrintMembers method of a derived record type calls the base implementation.派生记录类型的合成 PrintMembers 方法调用基本实现。 The result is that all public properties and fields of both derived and base types are included in the ToString output结果是派生类型和基类型的所有公共属性和字段都包含在 ToString output 中

  • Deconstructor behavior in derived records派生记录中的解构器行为

    The Deconstruct method of a derived record returns the values of all positional properties of the compile-time type.派生记录的 Deconstruct 方法返回编译时类型的所有位置属性的值。 If the variable type is a base record, only the base record properties are deconstructed unless the object is cast to the derived type.如果变量类型是基本记录,则只有基本记录属性被解构,除非 object 被强制转换为派生类型。

- Converting object is much performant! - 转换 object 非常高效!

Check this article检查这篇文章

MSSource MSSource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#asp.net Core中,Image的类型是什么? - What is the equivalent of the type Image in C# asp.net Core? 如何使用 Asp.net MVC 和 C# 从 SQL Server 搜索基于纬度和经度的记录 - How to search Latitude and Longitude based record from SQL Server use Asp.net MVC and C# CS0029 C# 无法将类型隐式转换为“int?” ASP.NET CORE 5 MVC 标识 - CS0029 C# Cannot implicitly convert type to 'int?' ASP.NET CORE 5 MVC Identity ASP.NET Core 5 MVC / C#:根据另一个 model 更改模型的值 - ASP.NET Core 5 MVC / C#: change model's value according to another model ASP.NET MVC 2:什么是MVC更像编码这个C#“后退”按钮的方式? - ASP.NET MVC 2: What's a more MVC like way to code this C# “Back” button? 我的 ASP.NET Core 按钮根本不发送任何内容(ASP.NET Core MVC 和 C#) - My ASP.NET Core button not sending anything at all (ASP.NET Core MVC and C#) 如何使用 C# 在 ASP.NET Core 3.1 MVC 中使用会话变量 - How to use session variables in ASP.NET Core 3.1 MVC using C# 使用外键表列显示数据 asp.net core mvc c# - Use foreign key table columns to display data asp.net core mvc c# 如何在 C# 页面 ASP.NET 核心 MVC 上使用登录管理器和用户管理器 - How to use SignIn Manager and User Manager on C# pages ASP.NET Core MVC ASP.NET CORE MVC C# 中的 GET 和 POST 问题 - GET and POST issues in ASP.NET CORE MVC C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM