- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace CSharp基础
- {
- class 值类型比较
- {
- public static void Main()
- {
- //值类型数据进行比较时,与引用类型规则相同
- //ReferenceEquals用于比较引用
- //Equals比较值
- //==可以看做是一个中间项
- int a = 100 ;
- int b = 100 ;
- //这样比较之所以为false,是因为,在方法里产生了装箱,隐式的!两个对象都会分别装箱,所以自然引用也不同
- Console.WriteLine( object.ReferenceEquals(a, b) ) ;//false
- //下面语句也是隐式装箱,不同的是System.ValueType已经重写了Eq方法,使他可以比较值
- Console.WriteLine( a.Equals(b) ); //true
- Console.ReadLine();
- }
- }
- }