# 引用类型和值类型的区分

//struct 这个是值类型
struct Fraction
{
public int numerator;
public int denominator;
}
class Class_02_06
{

public static void main(string []args)

{

Fraction x;
x.numerator=1;
x.denominator=2;
fraction y=x;
Console.WriteLine("x={0}/{1}",x.numerator,x.denominator);
Console.WriteLine("y={0}/{1}",y.numerator,y.denominator);
x.numerator=3;
y.denominator=3;
Console.WriteLine("x={0}/{1}",x.numerator,x.denominator);
Console.WriteLine("y={0}/{1}",y.numerator,y.denominator);
}

}
//运行的结果是 X=1/2 Y=1/2 X=3/2 Y=1/3

//class 是引用类型
class Fraction
{
public int numerator;
public int denominator;
}
class Class_02_08
{
public static void Main(string[] args)
{
Fraction x=new Fraction();
x.numerator=1;
x.denominator=2;
Fraction y=x;
Console.WriteLine("x={0}/{1}",x.numerator,x.denominator);
Console.WriteLine("y={0}/{1}",y.numerator,y.denominator);
x.numerator=3
Console.WriteLine("x={0}/{1}",x.numerator,x.denominator);
Console.WriteLine("y={0}/{1}",y.numerator,y.denominator);
y.denominator=3;
Console.WriteLine("x={0}/{1}",x.numerator,x.denominator);
Console.WriteLine("y={0}/{1}",y.numerator,y.denominator);
}
}

//运行结果是: 1/2 1/2 3/2 3/2 3/3 3/3

[@more@]引用类型的赋值是传递了一个引用给对方，无论是操作X，Y，实际上操作的是同一个东西

• 博文量
14
• 访问量
452520