先看简图:
C#中的运算符就有这些,简单介绍下C#中的赋值运算符。
class SimpleAssgningOP //定义类
{
public int i;
}
class RefTestApp
{
public static void main()
{
SimpleAssgningOP test1 =new SimpleAssgningOP ();
test1.i=1;
SimpleAssgningOP test2=new SimpleAssgningOP() ;
test2.i=2;
Console.WriteLine ("Before object assignment");
Console .WriteLine ("test1.i={0}",test1.i);
Console .WriteLine ("test2.i={0}",test2.i);
test1=test2;
Console .WriteLine ("After object assignment");
Console.WriteLine ("Before object assignment");
Console .WriteLine ("test1.i={0}",test1.i);
Console .WriteLine ("test2.i={0}",test2.i);
test1.i=150;
Console .WriteLine ("After change to only test1 member");
Console .WriteLine ("test1.i={0}",test1.i);
Console .WriteLine ("test2.i={0}",test2.i);
}
}
}
}
运算结果:
Before object assignment
test1.i=1
test2.i=2
After object assignment
Before object assignment
test1.i=2
test2.i=2
After change to only test1 member
test1.i=150
test2.i=150
注意:虽然只是改变了test1的值,但test2的值也改变了。