简体   繁体   English

是否有用于记录的公共基础 class?

[英]Is there a common base class for records?

In the .NET type system all reference types derive from System.Object , all value types from System.ValueType I think.在 .NET 类型系统中,所有引用类型都派生自System.Object ,我认为所有值类型都派生自System.ValueType Is there also a common base class all record types are derived from?是否还有一个共同的基础 class 所有record类型都派生自? If not, why not?如果不是,为什么不呢?

Is there also a common base class all record types are derived from?是否还有一个共同的基础 class 所有记录类型都派生自?

No, at least not anything specific to records.不,至少不是任何特定于记录的东西。 Records are just reference types.记录只是引用类型。 They can derive directly from System.Object or from other record types.它们可以直接从System.Object或其他记录类型派生。

If not, why not?如果不是,为什么不呢?

They don't require special handling by the runtime like value types do.它们不需要像值类型那样由运行时进行特殊处理。 Everything that's special about them is in the code that the compiler generates for them.它们的所有特殊之处都在编译器为它们生成的代码中。


"Root" Records “根”记录

A record can derive directly from System.Object , either implicitly or explicitly, just as any class can.记录可以直接从System.Object派生,无论是隐式还是显式,就像任何class That first level of record type bootstraps the internals of a record, establishing the root of a record type hierarchy.记录类型的第一级引导记录的内部,建立记录类型层次结构的根。

  1. Copy constructor -- A protected constructor that accepts a reference to the same type and copies its properties.复制构造函数——一个受保护的构造函数,它接受对同一类型的引用并复制其属性。 Derived records will call this from their copy constructors.派生记录将从其复制构造函数中调用它。

  2. Clone method -- It's essentially what ICloneable<T>.Clone() would have been had it ever been a part of the Base Class Library, but this one has an unaddressable name: <Clone>$ .克隆方法——如果它曾经是基本 Class 库的一部分,它本质上就是ICloneable<T>.Clone()的样子,但这个方法有一个无法寻址的名称: <Clone>$ When you use the with keyword, it calls <Clone>$ , which creates a new instance using the copy constructor, and then the compiler allows any init-only properties to be set within the initializer expression.当您使用with关键字时,它调用<Clone>$ ,它使用复制构造函数创建一个新实例,然后编译器允许在初始化表达式中设置任何仅初始化属性。

  3. Equality members -- This is the big one, since records are about value semantics .平等成员——这是最重要的,因为记录是关于值语义的。 The GetHashCode and Equals methods from System.Object are overridden, and IEquatable<T>.Equals() is implemented. System.Object中的GetHashCodeEquals方法被覆盖,并IEquatable<T>.Equals() There's also a protected property named EqualityContract which is used in the equality comparison to ensure that records of different types are not considered equal by comparing only the common parts.还有一个名为EqualityContract的受保护属性用于相等比较,以确保通过仅比较公共部分来确保不同类型的记录不相等。 The == and != operators are overloaded, as well. ==!=运算符也被重载。

  4. ToString method -- Overloaded to display the members of the record. ToString方法-- 重载以显示记录的成员。 It uses a protected PrintMembers method, which is called up the class hierarchy to build the string with all the properties' values.它使用受保护的PrintMembers方法,调用 class 层次结构来构建具有所有属性值的字符串。

  5. Deconstruct method -- Enables the record to be deconstructed into individual variables. Deconstruct方法——使记录能够被解构为单个变量。


Derived records派生记录

When you specify a base type (other than System.Object ) for a record type, it must be another record type because it requires the mechanisms established by the root of the record type hierarchy.当您为记录类型指定基本类型( System.Object )时,它必须是另一种记录类型,因为它需要记录类型层次结构的根建立的机制。 The derived record type defines the same members, but members that were marked virtual are now marked override .派生记录类型定义了相同的成员,但标记为virtual的成员现在标记为override

  1. Copy constructor -- This will call the base record's copy constructor.复制构造函数——这将调用基本记录的复制构造函数。

  2. Clone method -- <Clone>$ is overridden to return the more derived type using its copy constructor.克隆方法-- <Clone>$被重写以使用其复制构造函数返回更多派生类型。

Fun fact: This does not use the new covariant returns feature to declare a more derived return type on the overridden method, even though it returns a more derived type.有趣的事实:并没有使用新的协变返回特性在被覆盖的方法上声明一个更派生的返回类型,即使它返回了一个更派生的类型。 That's because 1) it doesn't need to, and 2) it would've made records unusable except in .NET 5.0 since support for the feature was added in the .NET 5.0 runtime.这是因为 1) 它不需要,2) 它会使记录无法使用,除非在 .NET 5.0 中,因为在 .NET 5.0 运行时中添加了对该功能的支持。

  1. Equality members -- The same methods are overridden and operators defined, but with any new properties considered for equality.平等成员——相同的方法被覆盖并定义了操作符,但任何新的属性都被认为是平等的。

  2. ToString method -- ToString and PrintMembers are overridden. ToString方法—— ToStringPrintMembers被覆盖。 ToString produces the actual class name and curly braces in the string. ToString在字符串中生成实际的 class 名称和花括号。 The base implementation is not called.不调用基本实现。 PrintMembers separately builds the property names and values. PrintMembers分别构建属性名称和值。

  3. Deconstruct method -- There's no overriding here because the base class's method isn't marked virtual . Deconstruct方法——这里没有覆盖,因为基类的方法没有标记为virtual If new properties are introduced, the signature is different from that of the base class so there's nothing to override.如果引入了新属性,则签名与基本 class 的签名不同,因此无需覆盖任何内容。 If no new properties are introduced, the method is marked new to shadow the one in the base class.如果没有引入新的属性,则将该方法标记为new的以隐藏基础 class 中的那个。


That is why a special base type for records isn't necessary.这就是为什么不需要记录的特殊基本类型的原因。


Special notes:特别说明:

When a record is marked sealed , members that normally would be protected virtual (but not protected override ) become private .当记录被标记为sealed时,通常protected virtual (但不受protected override )变为private How extensive that is depends on whether it derives from System.Object or another record type.这有多广泛取决于它是源自System.Object还是其他记录类型。

None of the above takes into account customization by way of providing your own implementation of any of the methods that are normally implemented by the compiler.以上都没有考虑通过提供您自己的任何通常由编译器实现的方法的实现来进行定制。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM