System.ValueType理解 [英] System.ValueType Understanding

查看:272
本文介绍了System.ValueType理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个值类型。

据我了解,建立一个结构会帮我。

我也试图获得从System.ValueType一种类型是一个抽象类。

但我得到一个编译器错误消息的 ..不能从特殊类派生System.ValueType

当我看到值类型的元数据,它看起来是一个普通抽象类。


  1. 什么做了特别的吗?


  2. 难道感知它的C#compilor特殊?


  3. 如果是这样,它推荐为编译器设计的规则?我的意思是它通用语言规范的一部分?



解决方案

值类型是一点点善意的谎言。

内置数字类型(int,长,字节),字符,枚举和结构是所有的值类型。

这意味着他们的身份和等价的概念,不同的对象类型。如果我做 X = Y x和y是引用类型,则x和y现在指向precisely同一个对象。但是,如果我这样做 X = Y x和y是值类型,则x和y现在碰巧是相同的两个完全不同的对象。 (这也反映在 == 等于,虽然这可以被覆盖)。

(这是人们获得通过谈论栈和堆突发奇想,如果他们有没有准备好,真的是一个实现细节,虽然很重要,是不是值类型和引用类型之间的区别点)。

现在,大部分是这一切,好的,但一件事引用类型的是,他们从从System.Object继承所有收益。值类型为int并没有真正的,这是好太多,因为它是在很多方面,它仅仅是四个字节由可爱的CPU指令是在这样做的这么好处理内存好得多。不过,这是要能够把一个int,如果它也从System.Object中继承有时是有用的,所以你可以。

当然,这意味着你可以做INT事情才有意义上一个System.Object做的,所以当你这样做的int是盒装,然后可以通过装箱了。

这是伟大的,但如果我们想要做具体的值类型的东西是什么?更重要的是,如果CLR的设计者们(特别是,他们想要一个GetHash code表示有关以上所描述的基于价值的对等,而不是基于身份的等价的对象具有值类型)是什么?

为此,我们有值类型。该系统将所有值类型从这个类,后者又继承自Object继承。枚举依次从值类型继承和所有enum类型继承它,允许所有枚举一些常见的功能。

所以,如果你想将所有的值类型的父类,用值类型,但如果你想真正创建一个值类型,创建一个结构或枚举适当。


通用类型系统的解释:


  

一个结构是从System.ValueType,而这又是从System.Object派生派生隐含值类型。 A型结构重新presenting值的内存需求小,传递值按值参数,具有强类型参数的方法非常有用。在.NET Framework类库,所有的原始数据类型(布尔,字节,字符,日期时间,小数,双,Int16的,的Int32,Int64的,为SByte,单身,UINT16,UInt32的,和UINT64)被定义为结构。


  
  

象类,结构同时定义数据(结构的字段),并且可以对数据执行的操作(该结构的方法)。这意味着,你可以调用结构的方法,包括对System.Object的和System.ValueType类中定义的虚方法,并在值类型本身定义的任何方法。换句话说,结构可具有字段,属性和事件,以及静态和非静态方法。您可以创建结构的实例,它们作为参数传递,存储为局部变量,或者将它们存储在另一个值类型或引用类型的字段。结构也可以实现接口。


  
  

值类型也由班在几个方面有所不同。首先,尽管他们含蓄从System.ValueType继承,但不能直接从任何类型的继承。同样地,所有的值类型是密封的,这意味着没有其他类型的可以从中得到。他们也不需要构造函数。


  
  

有关的每个值的类型,公共语言运行时提供的对应盒装类型,它是具有相同状态和行为作为值类型的类。当它传递给接受System.Object类型的参数的方法的值类型的实例是盒装。这是拆箱(即从一个类的实例回转换为数值类型的实例),当从接受的值类型作为by-reference参数的方法呼叫控制的回报。有些语言需要您使用时,需要的是盒装类型特殊语法;在需要时别人自动使用的盒装类型。当你定义一个值类型,你确定这些装箱和拆箱的类型。


的ValueType的奇异是允许上述情况发生。

I tried to create a ValueType.

I understand that creating a struct would help me.

I also tried to derive a type from System.ValueType which is an abstract class.

But I got a compiler error message ".. cannot derive from special class System.ValueType"

When I see the metadata of ValueType, it looks to be a regular abstract class.

  1. What made it special?

  2. Is it the C# compilor that senses it as special?

  3. If so, is it recommended as a rule for compiler design? I mean is it part of Common Language Specification?

解决方案

ValueType is a little white lie.

The built-in numeric types (int, long, byte), char, enums and structs are all value types.

This means that they have a different concepts of identity and equivalence to object types. If I do x = y and x and y are reference types, then x and y now point to precisely the same object. However, if I do x = y and x and y are value types, then x and y are now two completely different objects that happen to be identical. (This is also reflected in == and Equals, though that can be overridden).

(This is where people get sidetracked by talking about the stack and the heap, if they haven't already, really that's an implementation detail and while important, is not the point of the distinction between value and reference types).

Now, mostly this is all and good but one thing about reference types is that they all benefit from inheriting from System.Object. The value type int doesn't really, and that's good too as it's much better in many ways that it just be four bytes of memory handled by the lovely CPU instructions that are so good at doing so. Still, it's sometimes useful to be able to treat an int as if it also inherited from System.Object, and so you can.

Of course, this means that you may do things with int that only make sense to do on a System.Object, so when you do so the int is "boxed" and can then be "unboxed" again.

This is great, but what if we wanted to do something specific to value types? More to the point, what if the designers of the CLR did (in particular, they wanted a GetHashCode for value types that related to the value-based equivalence descibed above, rather than the identity-based equivalence that objects have)?

For this purpose we have ValueType. The system treats all value types as inheriting from this class, which in turn inherits from Object. Enum in turn inherits from value type and all enum types inherit from it, allowing some common functionality across all enums.

So, if you ever want to treat a superclass of all value types, use ValueType, but if you want to actually create a value type, create a struct or an enum as appropriate.


The Common Type System explanation:

A structure is a value type that derives implicitly from System.ValueType, which in turn is derived from System.Object. A structure is very useful for representing values whose memory requirements are small, and for passing values as by-value parameters to methods that have strongly typed parameters. In the .NET Framework class library, all primitive data types (Boolean, Byte, Char, DateTime, Decimal, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, and UInt64) are defined as structures.

Like classes, structures define both data (the fields of the structure) and the operations that can be performed on that data (the methods of the structure). This means that you can call methods on structures, including the virtual methods defined on the System.Object and System.ValueType classes, and any methods defined on the value type itself. In other words, structures can have fields, properties, and events, as well as static and nonstatic methods. You can create instances of structures, pass them as parameters, store them as local variables, or store them in a field of another value type or reference type. Structures can also implement interfaces.

Value types also differ from classes in several respects. First, although they implicitly inherit from System.ValueType, they cannot directly inherit from any type. Similarly, all value types are sealed, which means that no other type can be derived from them. They also do not require constructors.

For each value type, the common language runtime supplies a corresponding boxed type, which is a class that has the same state and behavior as the value type. An instance of a value type is boxed when it is passed to a method that accepts a parameter of type System.Object. It is unboxed (that is, converted from an instance of a class back to an instance of a value type) when control returns from a method call that accepts a value type as a by-reference parameter. Some languages require that you use special syntax when the boxed type is required; others automatically use the boxed type when it is needed. When you define a value type, you are defining both the boxed and the unboxed type.

The strangeness of ValueType is to allow the above to happen.

这篇关于System.ValueType理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆