现在的位置: 首页 > 综合 > 正文

C#中的值类型比较

2013年08月28日 ⁄ 综合 ⁄ 共 653字 ⁄ 字号 评论关闭
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CSharp基础
  6. {
  7.     class 值类型比较
  8.     {
  9.         public static void Main()
  10.         { 
  11.             //值类型数据进行比较时,与引用类型规则相同
  12.             //ReferenceEquals用于比较引用
  13.             //Equals比较值
  14.             //==可以看做是一个中间项
  15.             int a  = 100 ;
  16.             int b = 100 ;
  17.             //这样比较之所以为false,是因为,在方法里产生了装箱,隐式的!两个对象都会分别装箱,所以自然引用也不同
  18.             Console.WriteLine( object.ReferenceEquals(a, b) ) ;//false
  19.             //下面语句也是隐式装箱,不同的是System.ValueType已经重写了Eq方法,使他可以比较值
  20.             Console.WriteLine( a.Equals(b) ); //true
  21.             
  22.             Console.ReadLine();
  23.             
  24.         }
  25.     }
  26. }

抱歉!评论已关闭.