c#中对象类型的引用 [英] reference in object Type in c#

查看:113
本文介绍了c#中对象类型的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个具有object type的变量,我初始化了变量"d";然后分配了
将变量"d"更改为变量"h".最终更改了值变量"d",但是当对象为引用类型且变量"h"必须更改为新值时,变量"h"不会更改吗?

I create two variable with object type , i initialized variable "d" ;then i assign
variable "d" to variable "h".finally changed value variable "d",but variable "h" do not change while object is reference type and "h" must be change with new value?

object d = 5;
object h = d;
d = 9;        //h variable do not change!!

推荐答案

变量的类型是引用类型System.Object(而object是语言的别名),是所有类型的共同基础.整数之类的原始类型会发生什么?

使用引用类型,您可以存储两个对象:变量本身存储在变量中,而引用指向存储在堆中的某个对象.分配时,引用已更改,对象未更改.

变量具有一些编译时类型,该类型从不更改,并且仅由声明定义. 它的编译时类型永远不会改变,但是运行时类型可以在运行时进行更改.类型System.Object的所有变量都是与赋值兼容的,且具有任何类型(在赋值运算符的右侧),但非引用类型是 box ,请参见 http://en.wikipedia.org/wiki/Boxing_%28computer_science%29#Boxing [ ^ ],http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx [
The type of your variables is a reference type System.Object (and object is an alias in language), a common base of all types. What happens with primitive types like integers?

Working with reference types, you store two objects: the variable itself stored in a variable, and the reference points to some object stored in heap. When you assign, reference is changed, object is not.

A variable has some compile-time type which never changes and is defined only by a declaration. Its compile-time type never changes, but run-time type can be changed during run time. All variable of the type System.Object are assignment-compatible with any type (on the right of the assignment operator), but non-reference types are boxes, see http://en.wikipedia.org/wiki/Boxing_%28computer_science%29#Boxing[^], http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx[^].

Here is what happened:

In fist line, a boxed form of 5 is created and assigned to d.

In second line, h is done referentially equal to d. You obtain one single boxed form of 5 referenced by the two variables.

In third line, a boxed form of 9 is created and assigned to d. It created a brand new reference, not modified a previously created boxed form.

Assignment to variable of reference types in general case change the reference, not the value of anything.

The boxed object is immutable from the standpoint of the language, but in principle you can modify it using Reflection.

—SA


我必须不同意以上J_N的回答:值将类型装箱,然后将其装箱:请参阅:[
I have to disagree with the answer by J_N above: value Types are boxed, and unboxed: see: [^].

A reference Type remains a reference Type: consider this simple example:
public class SomeClass
{
    public string someString { get; set; }
}

现在执行以下代码:

SomeClass sClass = new SomeClass();

SomeClass dClass = sClass;

sClass.someString = "hello";

dClass.someString = "goodbye";

,并在代码中的最后一条语句之后设置一个断点.

您会看到更改dClass中的属性``someString''的字符串值(已设置为等于sClass的实例):也更改了sClass中的属性somesome的字符串值. >
这是因为sClass实例和dClass中保存的指向sClass实例的指针:都引用了相同的基础对象.

And set a break-point right after the last statement in the code.

You will see that changing the string value of the Property ''someString in ''dClass which has been set equal to the instance of ''sClass: also changes the string value of the Property ''someString in ''sClass.

This is because both the instance of ''sClass, and the pointer to the instance of ''sClass held in ''dClass: are both references to the same underlying Object.


这篇关于c#中对象类型的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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