您的位置:首页 > 编程语言 > C#

CSharp - Value types vs reference types

2013-06-03 04:12 295 查看
/*

Author: Jiangong SUN

*/

public class CompareValueTypeAndReferenceType
{
public class ReferenceClass
{
public string Value { get; set; }
}
public struct Structure
{
public string Value { get; set; }
}

public static void Main()
{
ReferenceClass ref1 = new ReferenceClass();
ref1.Value = "ref1";
ReferenceClass ref2 = new ReferenceClass();
ref2.Value = "ref2";

ref2 = ref1;
Console.WriteLine(ref1.Value);
Console.WriteLine(ref2.Value);

ref2.Value = "ref3";
Console.WriteLine(ref1.Value);
Console.WriteLine(ref2.Value);

Structure struct1 = new Structure();
struct1.Value = "struct1";
Structure struct2 = new Structure();
struct2.Value = "struct2";

struct2 = struct1;
Console.WriteLine(struct1.Value);
Console.WriteLine(struct2.Value);

struct2.Value = "struct3";
Console.WriteLine(struct1.Value);
Console.WriteLine(struct2.Value);

Console.ReadLine();
}
}


Results:



References:
http://csharp-station.com/Tutorial/CSharp/Lesson22
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: