【发布时间】:2014-05-10 19:49:53
【问题描述】:
classes deal with the reference types and traditional data types deal with the value type just for example :
int i=5;
int j=i;
i=3 ; //then this will output i=3 and j=5 because they are in the different memory blocks .
类似地,如果我们谈论一个类的对象,比如点类
class point
{
public int x,y;
void somefucnt(point p,int x)
{
Console.writeline("value of x is "+p.x);
x=22;
Console.writeline("value of x is "+p.x);
}
}
class someotherclass
{
static void Main(string [] args )
{
p1.x=10;
p1.somefunct(p1,p1.x);
}
}
尽管我将 x 更改为其他值,但两个 console.write 语句都打印 10 ?为什么会这样?因为 p 只是对 x 的引用,所以它应该通过改变 x 的值来更新。这件事真的让我很困惑。
【问题讨论】: