using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 值类型和引用类型
{
/// <summary>
/// 类
/// </summary>
class TestClass
{
public int Id;
public string Name;
public List<int> lint;
}
class TestClass1
{
public int Id;
public string Name;
public List<int> lint;
}
/// <summary>
/// 结构体
/// </summary>
struct TestStruct
{
public int Id;
public string Name;
}
internal class Program
{
static void Main(string[] args)
{
//测试引用类型
TestClass c1 = new TestClass { Id = 0, Name = "未定义" };
TestClass c2 = c1;//c1给了c2 ,这里注定了C1和C2 无论后面用哪一个都是唯一的地址
//无论是改变C1或者C2 都会改变引用地址的值,这个地址是这个对象的唯一
c2.Id = 1; c2.Name = "a";
c1.Id = 123; c1.Name = "aBC";
c1.lint = new List<int>();
c1.lint.Add(12);
//这时输出的时最后一次赋值的信息,无论时c1还是c2他们都是一个地址
Console.WriteLine($"c1[{c1.Id},{c1.Name}]");
Console.WriteLine($"c2[{c2.Id},{c2.Name}]");
TestClass1 testClass1 = new TestClass1();
//把C1的所有变量赋值给Testclass
//值类型的赋值
testClass1.Id = c1.Id; testClass1.Name = c1.Name;
//引用类型的赋值
testClass1.lint = c1.lint;
//当我们在赋值后更改值
c1.Id = 777; c1.Name = "aaa";
c1.lint[0]=199;
//打印就会出现只有testClass1.lint[0]的值有变化,其他的值类型无变化
Console.WriteLine($"testClass1[{testClass1.Id},{testClass1.Name},{testClass1.lint[0]}]");
//测试值类型
TestStruct s1 = new TestStruct { Id = 0, Name = "未定义" };
TestStruct s2 = s1;//s1给了s2
s2.Id = 2; s2.Name = "b";
Console.WriteLine($"s1[{s1.Id},{s1.Name}]");
Console.ReadKey();
}
}
//OUTPUT
// c1[123, aBC]
//c2[123, aBC]
//testClass1[123, aBC, 199]
//s1[0, 未定义]
}