请帮我理解类型对象的一些c#basic(ref与值,拳击) [英] Please help me to understand some c# basic's on type object (ref vs. value, boxing)
问题描述
我正在尝试理解c#中的一些基础知识。我确信我理解了引用类型和值类型的基础知识。但在MSDN中阅读后,我变得越来越困惑(以下两个链接只能解释我为什么得出结论,例如骨头):
http://msdn.microsoft.com/de-de/library/490f96s2.aspx [ ^ ]
对象类型是Object的别名
- >我希望理解正确
类型对象是参考类型的_build_
- >对象是引用类型
http://msdn.microsoft.com/de-de/library/9kkx3h3c.aspx [ ^ ]
...在C#的统一类型系统中,所有类型,预定义的和用户定义的,引用类型和值类型,直接或间接地从Object继承。您可以将任何类型的值分配给object类型的变量。当一个值类型的变量转换为object时,它被称为盒装。
- >混淆:引用类型和值类型,直接或间接地从Object继承。
示例
从上面我总结说,对象可以是像两者一样并尝试(也许是天真)跟随,_assuming_我使用对象是一种引用类型:
I'm trying to understand some basics in c#. I'm convinced that I understand the basics of reference types and value types. But after reading in MSDN I'm becoming more and more confused (the following two links do only explain why I came to my conclusion, Example is the bone):
http://msdn.microsoft.com/de-de/library/490f96s2.aspx[^]
The object type is an alias for Object
--> That I hopefully understand right
Type object is a _build in reference type_
--> object is a reference type
http://msdn.microsoft.com/de-de/library/9kkx3h3c.aspx[^]
...In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. You can assign values of any type to variables of type object. When a variable of a value type is converted to object, it is said to be boxed.
--> confusion: reference types and value types, inherit directly or indirectly from Object.
Example
From the above I concluded that object can be "something like both" and tried the (maybe naiv) following, _assuming_ I'm using object is a reference type:
int int1 = 1;
object boxint1 = int1; // here also tried boxint1= new object()...same result
object boxint2 = boxint1;
boxint1 = (int)boxint1 + 1;
boxint2 = (int)boxint2 + 1;
// At this point I expected that
// boxint1 is equal to 2
// boxint2 is equal to 3
// but both are equal to 2
例如 http://msdn.microsoft.com/de-de/library/yz2be5wk.aspx [ ^ ]包含一个很好的图形内存情况是盒装类型
有人可以帮助我解决这个令人困惑的问题。解释一下我的理解/误解是完全错的?
非常感谢advacne。
Bruno
e.g. http://msdn.microsoft.com/de-de/library/yz2be5wk.aspx[^]contains a nice graphic how memory situation is for boxed types
Can somebody help me out of this confusing resp. explain me what I'm understanding/misinterpretingthat completely wrong?
Thank you very much in advacne.
Bruno
推荐答案
这可能看起来令人困惑,但实际上相当简单。 :)
首先考虑引用类型:
This might look confusing, but it's actually fairly simple. :)
Start by thinking about reference types:
Person a = new Person("Bruno");
Person b = a;
此时, a
和 b
指向同一个对象。
At this point, both a
and b
are pointing to the same object.
a = new Person("Richard");
现在 a
指向另一个对象。如果 b
也改为指向新对象,那将是非常令人惊讶的!
对于普通值类型,适用相同的语义:
Now a
is pointing to a different object. It would be quite surprising if b
was also changed to point to the new object!
With normal value types, the same semantics apply:
int a = 1;
int b = a;
a = 2;
如果在运行此代码后<$ c,那将是令人惊讶的$ c> b 已更改为 2
。
使用盒装值类型,适用相同的语义。装箱值类型有效地创建了一个新的盒子来存储盒装值的副本。如果在同一个变量中存储一个新的盒装值,它不会自动影响指向同一个盒子的其他变量。
It would be surprising if, after running this code, b
had been changed to 2
.
With boxed value types, the same semantics apply. Boxing a value type effectively creates a new "box" to store a copy of the value being boxed. If you store a new boxed value in the same variable, it doesn't automatically affect other variables pointing to the same box.
int int1 = 1;
object boxint1 = int1; // Imagine: object boxint1 = new BoxedInteger(int1);
object boxint2 = boxint1; // Now boxint1 and boxint2 point to the same box.
boxint1 = (int)boxint1 + 1; // Imagine: boxint1 = new BoxedInteger((int)boxint1 + 1);
// Would you expect boxint2 to be changed here?
这篇关于请帮我理解类型对象的一些c#basic(ref与值,拳击)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!