简体   繁体   English

可以从c#中的类派生结构吗?

[英]can a struct be derived from a class in c#?

Can a struct be derived from a class in c#? 结构可以从c#中的类派生吗?

If not, Why can primitive data types, such as int, be derived from the class object ? 如果没有,为什么原始数据类型(如int)可以从类object派生? Since the data type int is basically a struct type(value type). 由于数据类型int基本上是结构类型(值类型)。

Is this just an exception to the rule? 这仅仅是规则的例外吗?

Integers and other value types (eg bool) are objects, because it allows them to leverage inheritance (ie they have access to the common .Equals() , .GetType() , .ToString() functions). 整数和其他值类型(例如bool)是对象,因为它允许它们利用继承(即它们可以访问公共.Equals() .GetType() .Equals() .GetType() .ToString()函数)。

It's a design decision in the .NET framework. 这是.NET框架中的设计决策。 Rather than writing separate functions for all the value types under System.ValueType, they use a common code base. 它们不是为System.ValueType下的所有值类型编写单独的函数,而是使用公共代码库。

Microsof's document on Types Microsof关于类型的文档

All structs inherit System.ValueType , which in turn inherits Object . 所有结构都继承System.ValueType ,而System.ValueType又继承Object

You cannot change that. 你不能改变它。

When the run-time allocates a storage location for a type, or generates code to operate on one, it checks whether the type derives from System.ValueType but is not System.ValueType itself. 当运行时为类型分配存储位置或生成代码以对其进行操作时,它会检查类型是否派生自System.ValueType但不是System.ValueType本身。 Unless the storage location meets those criteria, it will hold a heap object reference, and any code to operate on its members (fields, methods, properties, etc.) will act upon the referenced object. 除非存储位置满足这些条件,否则它将保存堆对象引用,并且对其成员(字段,方法,属性等)进行操作的任何代码将对引用的对象起作用。 Otherwise, the storage location will hold all the public and private fields of that type (which will be laid out identically in all storage locations of that type), and any code to operate on its members will operate on the storage location itself. 否则,存储位置将保存该类型的所有公共和私有字段(将在该类型的所有存储位置中相同地布置),并且对其成员进行操作的任何代码将在存储位置本身上操作。

If an attempt is made to store a value type into a storage location of class ValueType , or a storage location which does not derive from ValueType , the system will generate a new heap object of the storage location's type, and then store a reference to that object in the appropriate storage location. 如果尝试将值类型存储到ValueType类的存储位置,或者存储位置不是从ValueType派生,则系统将生成存储位置类型的新堆对象,然后存储对该类型的引用对象在适当的存储位置。 Although storage locations of types deriving from System.ValueType , and code to access them, are treated specially by the run-time, heap object instances which inherit from System.ValueType (such as the newly-created one just mentioned) are simply heap objects that happen to derive from System.ValueType , and have inheritance behaviors which are essentially the same as other types, and may thus be passed around by code expecting to deal with heap references. 虽然从System.ValueType派生的类型的存储位置和访问它们的代码由运行时特别处理,但是从System.ValueType继承的堆对象实例(例如刚刚提到的新创建的实例)只是堆对象碰巧派生自System.ValueType ,并且具有与其他类型基本相同的继承行为,因此可能由期望处理堆引用的代码传递。

A statement like Object Foo = New System.Drawing.Point(3,4); Object Foo = New System.Drawing.Point(3,4);类的语句Object Foo = New System.Drawing.Point(3,4); actually involves three kinds of things: 实际上涉及三种事情:

  1. An unnamed temporary storage location of type `System.Drawing.Point(3,4)` which holds the private fields of that type (two integers), and is initialized to (3,4). 一个未命名的临时存储位置,类型为`System.Drawing.Point(3,4)`,它保存该类型的私有字段(两个整数),并初始化为(3,4)。 Note that this storage location does not hold an `Object`; 请注意,此存储位置不包含“对象”; it holds two integers which the compiler knows represent its fields. 它包含两个整数,编译器知道它们代表它的字段。
  2. A heap object of type `System.Drawing.Point`. 一个类型为`System.Drawing.Point`的堆对象。 Because this is a heap object, it inherits from `System.Object`, as do all heap objects. 因为这是一个堆对象,所以它继承自`System.Object`,所有堆对象都是如此。
  3. A storage location of type `Object`, which is represented by the variable name `Foo`. “对象”类型的存储位置,由变量名称“Foo”表示。

The upshot of all this is that while value types may be defined as inheriting from ValueType which inherits from Object , and while heap objects whose types inherit from ValueType do inherit from Object , storage locations of value types do not hold things that inherit from Object . 所有这些的结果是,虽然值类型可以定义为从继承自Object ValueType继承,而类型继承自ValueType堆对象继承自Object ,但值类型的存储位置不包含从Object继承的东西。

The class hiearchy works as follows (simplified): 类层次结构的工作原理如下(简化):

Object -> ValueType -> int Object -> ValueType -> struct Object - > ValueType - > int Object - > ValueType - > struct

Structs by definition of c# do not allow inheritance. 根据c#的定义,结构不允许继承。

Here is a nice article describing the role of stucts within the C# language: 这是一篇很好的文章,描述了st#在C#语言中的作用:

http://msdn.microsoft.com/en-us/library/aa288471(v=vs.71).aspx http://msdn.microsoft.com/en-us/library/aa288471(v=vs.71).aspx

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

相关问题 C#可以从派生类调用基类属性吗 - C# Can a base class property be invoked from derived class 您能否将派生类添加到其基类列表中,然后从C#中的基类列表中调用派生类的方法 - Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C# C#指定派生字段是从其原始类派生的 - C# Specify that a derived field is derived from its original class C#:如何从派生类设置属性的最大值? (而不是在基类中) - C#: How can I set a maximal Value of a property from a derived class? (And not in base class) 为什么我不能将一个隐式运算符从Base类写入C#中的Derived类? - Why can't I write an implicit operator from a Base class to a Derived class in C#? 为什么我可以从基于C#/ C ++中的类型的模板化/泛型类派生 - Why can I derived from a templated/generic class based on that type in C# / C++ 为什么Struct不能从另一个struct派生? - Why a Struct can not be derived from another struct? C#:如何在TextBox派生的类中从事件处理程序引发异常? - C#: How can I raise an exception from an event handler in a class derived from TextBox? C#将相同的属性从派生类克隆到基类 - C# Clone same properties from Derived class to base class 如何从派生类C#访问基类值 - How to access a base class value from derived class c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM