值类型(变量直接包含数据) | 简单类型 |
枚举 | |
结构 | |
引用类型(变量包含对存储对象的引用) | 类 |
接口 | |
委托 | |
数组 |
这两者类型有什么区别呢?很简单。值类型是直接包含了变量的具体值,引用类型包含的是指针(至少你可以这么理解)
他们的区别看以下的代码就立即明了
1
int i1 = 10;
2
int i2 = i1;
3
System.Console.WriteLine(i1.Equals(i2));//值相等
4
System.Console.WriteLine(object.ReferenceEquals(i1,i2));//引用不相等
5
object o1 = new object();
6
object o2 = o1;
7
System.Console.WriteLine(o1.Equals(o2));//值相等
8
System.Console.WriteLine(object.ReferenceEquals(o1, o2));//引用相等

2

3

4

5

6

7

8

要记住哪些不是引用类型更简单了:除了类、接口、委托和数组外,其他都是值类型。