{
int a = 9; //給變量a賦值為9
int b = a; //將a的副本給變量b
b = 10;
Console.WriteLine(string.Format("a={0},b={1}", a, b));
Person ZS = newPerson(); //張三
ZS.Age = 99; //張三的年齡是99
Person SM = ZS; //三毛等于張三,即張三和三毛就是同一個人
SM.Age = 100; //三毛年齡變成100,張三也就變成了100
Console.WriteLine(string.Format("A={0},B={1}", ZS.Age, SM.Age));
Console.ReadKey();
}
}
classPerson
{
publicint Age { get; set; }
}
相同的結構,不同的結果。
證明string是引用類型
classProgram
{
staticvoid Main()
{
int n = 99;
Console.WriteLine("Before:n={0}", n.GetHashCode());
//此時獲取到的哈希碼值就是n的變量值
GetInt(n);
string s = "Hello";
Console.WriteLine("Before:s={0}", s.GetHashCode());
GetString(s);
Console.ReadKey();
}
staticint GetInt(int n)
{
Console.WriteLine("After:m={0}", n.GetHashCode());
//傳過來的是變量值,說明這是值傳遞
return n;
}
staticstring GetString(string s)
{
Console.WriteLine("After:s={0}", s.GetHashCode());
//傳過來的是地址而不"Hello",說明這時引用傳遞
return s;
}
}
新聞熱點
疑難解答