如何从值类型派生的对象(引用类型),仍然是值类型? [英] How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?

查看:206
本文介绍了如何从值类型派生的对象(引用类型),仍然是值类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#不允许结构从类派生,但所有的值类型从Object派生。这哪里是优等生?

如何在CLR处理呢?

解决方案
  

C#不允许结构从类派生

您的说法是不正确的,因此你的困惑。 C#中的没有的允许结构从类派生。所有的结构从同一类System.ValueType,派生从System.Object派生。而所有枚举从System.Enum派生。

  

这哪里是优等生?

它是由在世界各地的编译器上运行。 : - )

说真的,我不明白的问题。你说的这里是什么意思?

  

如何在CLR处理呢?

非常好。 : - )

此外,我不明白你在问什么。是什么让一个值输入一个值类型是其实例是按值复制。是什么让一个引用类型的引用类型,它的实例是按引用复制。你似乎有一些信念,在继承值类型和引用类型之间的关系有些特殊和不寻常的,但我不明白是什么信念。 继承无关,与事情是如何复制的。

看它这样。假设我告诉你下面的事实:

  • 有两种盒,红 箱和蓝箱。

  • 每一个红色的盒子是空的。

  • 有三个特殊的蓝盒子叫Ò,V和E。

  • O不再是内部的任何框。

  • V是内部O操作。

  • E是里面诉

  • 没有其他的蓝盒子里面诉

  • 没有蓝盒子里面即

  • 每一个红色的盒子是在任一V或即

  • 每个蓝色方块为O另一种是自身内部一个蓝色的盒子。

蓝色的盒是引用类型,红色的盒是值类型,O是System.Object的,V是System.ValueType,E是System.Enum,而内部关系源自。

这是一个完全一致的,简单的集合,你可以很容易地实现自己,如果你有很多纸板和很大的耐心规则。无论是盒子是红色或蓝色无关,与它的里面;在现实世界中这是完全可能把一个红色的盒子里面发现一个蓝色的盒子。在CLR,这是完全合法的,以使该继承引用类型,只要它可以是System.ValueType和System.Enum值类型。

因此​​,让我们重新表述您的问题:

  

如何从值类型派生的对象(引用类型),仍然是值类型?

  

这怎么可能,每一个红色方块(值类型)是内(源于)中O(System.Object的),这是一个蓝色的盒子(引用类型),仍然是一个红色的盒子(数值型) ?

当你句话吧这样的,我希望这是显而易见的。没有什么东西把一个红色的盒包装盒V,这是机箱内0,这是蓝色的内阻止你。为什么会有?

C# doesn't allow structs to derive from classes, but all ValueTypes derive from Object. Where is this distinction made?

How does the CLR handle this?

解决方案

C# doesn't allow structs to derive from classes

Your statement is incorrect, hence your confusion. C# does allow structs to derive from classes. All structs derive from the same class, System.ValueType, which derives from System.Object. And all enums derive from System.Enum.

Where is this distinction made?

It's made everywhere in the world that the compiler runs. :-)

Seriously, I don't understand the question. What do you mean by "where"?

How does the CLR handle this?

Extremely well. :-)

Again, I don't understand what you're asking. What makes a value type a value type is that its instances are copied by value. What makes a reference type a reference type is that its instances are copied by reference. You seem to have some belief that the inheritance relationship between value types and reference types is somehow special and unusual, but I don't understand what that belief is. Inheritance has nothing to do with how things are copied.

Look at it this way. Suppose I told you the following facts:

  • There are two kinds of boxes, red boxes and blue boxes.

  • Every red box is empty.

  • There are three special blue boxes called O, V and E.

  • O is not inside any box.

  • V is inside O.

  • E is inside V.

  • No other blue box is inside V.

  • No blue box is inside E.

  • Every red box is in either V or E.

  • Every blue box other than O is itself inside a blue box.

The blue boxes are reference types, the red boxes are value types, O is System.Object, V is System.ValueType, E is System.Enum, and the "inside" relationship is "derives from".

That's a perfectly consistent and straightforward set of rules which you could easily implement yourself, if you had a lot of cardboard and a lot of patience. Whether a box is red or blue has nothing to do with what it's inside; in the real world it is perfectly possible to put a red box inside a blue box. In the CLR, it is perfectly legal to make a value type that inherits from a reference type, so long as it is either System.ValueType or System.Enum.

So let's rephrase your question:

How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?

as

How is it possible that every red box (value types) is inside (derives from) box O (System.Object), which is a blue box (a reference Type) and still be a red box (a value type)?

When you phrase it like that, I hope it's obvious. There's nothing stopping you from putting a red box inside box V, which is inside box O, which is blue. Why would there be?

这篇关于如何从值类型派生的对象(引用类型),仍然是值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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