[英]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下的所有值类型编写单独的函数,而是使用公共代码库。
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: 实际上涉及三种事情:
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.