一、运算符 1.checked和unchecked运算符 启用异常检查可以使用checked,禁用溢出检查可以使用unchecked. 例如: byte b=255; unchecked { b++; } Console.WriteLine(b.ToString()); 以上代码不会抛出异常,但会丢失数据,最后得到的值为0。 2.is运算符 is运算符可以检查对象是否与特定的类型兼容,或者派生自该类型。 int i=10; if(i is object) { Console.WriteLine("i is an object"); } 3.as运算符 as运算符用于执行引用类型的显示类型转换。如果要要转换的类型与指定的类型兼容,转换会成功执行;如果类型不兼容,as运算符就会返回null值。 string o="Some String"; string s1=o1 as string;//s1="Some String" 4.sizeof运算符 sizeof运算符可以确定栈中值类型需要的长度(单位是字节)。如果是复杂类型 的话需要把代码放到unsafe块中。 例如: Console.WriteLine(sizeof(int)); unsafe { Console.WriteLine(sizeof(Customer)); } 5.typeof运算符 typeof运算符返回一个表示特定类型的System.Type对象。typeof(string)表示返回System.String类型的Type对象。使用反射技术动态查找对象的相关信息时,这个运算符很有用。 6.可空类型运算符 int? 比较可空类型时,只要其中一个操作数是null,就返回false. 7.空合并运算符 (??) 第一个操作数必须是一个可空类型或者一个引用类型,第二个操作数必须与第一个类型相同或者可以饮食转换为第一个类型。 例如: int?a=null; int b; b=a??10; 显示结果为:10 二、类型的安全性 三、比较对象的相等性 1.比较引用类型的相等性 1).ReferenceEquals()方法 它是一个静态方法,如果提供的两个引用引用同一个对象,则返回true,否则false.他认为null等于null. 例如: SomeClass x,y; x=new SomeClass(); y=new SomeClass(); bool b=ReferenceEquals(x,y); 2).虚拟的Equals()方法 允许重载。 3).静态的Equals()方法 4).比较运算符 == 2.比较值类型的相等性 一般用System.ValueType提供的Equal()方法,但可以对其进行重写。 四、运算符的重载 例如: namespace VectorStruct { internal class MainEntryPoint { private static void Main() { Vector vect1, vect2, vect3; vect1 = new Vector(3.0, 3.0, 1.0); vect2 = new Vector(2.0, -4.0, -4.0); vect3 = vect1 + vect2; Console.WriteLine("vect1 = " + vect1.ToString()); Console.WriteLine("vect2 = " + vect2.ToString()); Console.WriteLine("vect3 = " + vect3.ToString()); Console.ReadLine(); } } } namespace Wrox.ProCSharp.OOCSharp { struct Vector { public double x, y, z; public Vector(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } public Vector(Vector rhs) { x = rhs.x; y = rhs.y; z = rhs.z; } public override string ToString() { return "( " + x + " , " + y + " , " + z + " )"; } public static Vector operator +(Vector lhs, Vector rhs) { Vector result = new Vector(lhs); result.x += rhs.x; result.y += rhs.y; result.z += rhs.z; return result; } } } 上例中定义了的operator +方法对运算符进行了重载。C#要求所有的运算符重载都必须声明为public和static,这表示他们不能与某个特定的实例相关联,所以运算符重载的代码不能访问非静态类成员。 五、用户自定义的类型强制转换 例如: class Program { struct Currency { public uint Dollars; public ushort Cents; public Currency(uint dollars, ushort cents) { this.Dollars = dollars; this.Cents = cents; } public override string ToString() { return string.Format("${0}.{1,-2:00}", Dollars,Cents); } public static implicit operator float(Currency value) { return value.Dollars + (value.Cents / 100.0f); }//对float运算进行重载 public static explicit operator Currency(float value) { uint dollars = (uint)value; ushort cents = (ushort)((value - dollars) * 100); return new Currency(dollars, cents); } } static void Main(string[] args) { try { Currency balance = new Currency(50,35); Console.WriteLine(balance); Console.WriteLine("balance is " + balance); Console.WriteLine("balance is (using ToString()) " + balance.ToString()); float balance2= balance;//对balance用自定义的强制转化进行转换 Console.WriteLine("After converting to float, = " + balance2); balance = (Currency) balance2; Console.WriteLine("After converting back to Currency, = " + balance); Console.WriteLine("Now attempt to convert out of range value of " + "-$50.50 to a Currency:"); checked { balance = (Currency) (-50.50); Console.WriteLine("Result is " + balance.ToString()); } } catch(Exception e) { Console.WriteLine("Exception occurred: " + e.Message); } Console.ReadLine(); } } 显示结果 class Program { struct Currency { public uint Dollars; public ushort Cents; public Currency(uint dollars, ushort cents) { this.Dollars = dollars; this.Cents = cents; } public override string ToString() { return string.Format("${0}.{1,-2:00}", Dollars,Cents); } public static implicit operator float(Currency value) { return value.Dollars + (value.Cents / 100.0f); } public static explicit operator Currency(float value) { uint dollars = (uint)value; ushort cents = (ushort)((value - dollars) * 100); return new Currency(dollars, cents); } } static void Main(string[] args) { try { Currency balance = new Currency(50,35); Console.WriteLine(balance); Console.WriteLine("balance is " + balance); Console.WriteLine("balance is (using ToString()) " + balance.ToString()); float balance2= balance; Console.WriteLine("After converting to float, = " + balance2); balance = (Currency) balance2; Console.WriteLine("After converting back to Currency, = " + balance); Console.WriteLine("Now attempt to convert out of range value of " + "-$50.50 to a Currency:"); checked { balance = (Currency) (-50.50);//此处会丢失数据。 Console.WriteLine("Result is " + balance.ToString()); } } catch(Exception e) { Console.WriteLine("Exception occurred: " + e.Message); } Console.ReadLine(); } }