本文共 4185 字,大约阅读时间需要 13 分钟。
☆C#的运算符定义只有四种形式:--------------------------------------- ①public static 返回类型 operator ?(单形参) ②public static 返回类型 operator ?(双形参) ③public static implicit operator 隐转目标类型(单源类型形参) ④public static explicit operator 显转目标类型(单源类型形参) △全部都是传值,故不可用ref和out参数 △全部都是static △比较运算符的重载必须贯彻成对一起定义的原则来实现C#的一站式编码 △true()和false()也是运算符,分别表示 非(假/空)吗 和 非(真/空)吗 ☆可重载的运算符以及解决方法可行性表(摘自vs.n中文正式版帮助):-------------------------------- 运算符 可重载性 +, -, !, ~, ++, --, true(), false() 可以重载这些一元运算符。 +, -, *, /, %, &, | , ^, <<, >> 可以重载这些二进制运算符。 ==, !=, <, >, <=, >= 可以重载这些比较运算符(但必须成对重载)。 &&, || 不能重载条件逻辑运算符,但可使用 & 和 | 对其进行计算,可以重载 & 和 |;请参阅 7.11.2 用户定义的条件逻辑运算符。 [] 不能重载数组索引运算符,但可定义索引器。 () 不能重载转换运算符,但可定义新的转换运算符(请参阅 explicit 和 implicit)。 +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= 不能重载赋值运算符,但例外的是可使用 +(该运算符可被重载)计算 +=。 =, ., ?:, ->, New, is, sizeof(), TypeOf() 不能重载这些运算符。 ☆一些散题:------------------------------------- ①传递对象引用是传值的一种,不属于传址.因为对象的值只是实体的地址,如果传对象变量的地址才算是传址 ②readonly比const宽松,因为也可以在构造器中赋值,此外【static readonly】<=>【const】 ③using简化书写的其中一个功能就是创建〖姓或类型〗的别名,可减少修改 ④using(IDispose化的对象〖,....〗){ }语句可以确保句尾调用对象.Dispose() ⑤代码和注释是最好的心得! ☆以下是C#运算符重载以及其他技巧的简单例子(摘自vs.n中文正式版帮助)-------------------------------- //版权所有 (C) 2000 Microsoft Corporation。保留所有权利。 // dbbool.cs using System; public struct DBBool { // 3 个可能的 DBBool 值: public static readonly DBBool dbNull = new DBBool(0); public static readonly DBBool dbFalse = new DBBool(-1); public static readonly DBBool dbTrue = new DBBool(1); // 为 dbFalse、dbNull、dbTrue 存储 -1、0、1 的私有字段: int value; // 私有构造函数。值参数必须为 -1、0 或 1: DBBool(int value) { this.value = value; } // 从 bool 到 DBBool 的隐式转换。将 true 映射为 // DBBool.dbTrue,并将 false 映射为 DBBool.dbFalse: public static implicit operator DBBool(bool x) { return x? dbTrue: dbFalse; } // 从 DBBool 到 bool 的显式转换。如果 // 给定的 DBBool 为 dbNull,则引发异常,否则返回 // true 或 false: public static explicit operator bool(DBBool x) { if (x.value == 0) throw new InvalidOperationException(); return x.value > 0; } // 相等运算符。如果任何一个操作数为 dbNull,则返回 dbNull, // 否则返回 dbTrue 或 dbFalse: public static DBBool operator ==(DBBool x, DBBool y) { if (x.value == 0 || y.value == 0) return dbNull; return x.value == y.value? dbTrue: dbFalse; } // 不等运算符。如果任何一个操作数为 // dbNull,则返回 dbNull,否则返回 dbTrue 或 dbFalse: public static DBBool operator !=(DBBool x, DBBool y) { if (x.value == 0 || y.value == 0) return dbNull; return x.value != y.value? dbTrue: dbFalse; } // 逻辑非运算符。如果操作数为 // dbFalse,则返回 dbTrue,如果操作数为 dbNull,则返回 dbNull,如果 // 操作数为 dbTrue,则返回 dbFalse: public static DBBool operator !(DBBool x) { return new DBBool(-x.value); } // 逻辑 AND 运算符。如果任何一个操作数为 // dbFalse,则返回 dbFalse,如果任何一个操作数为 dbNull,则返回 dbNull,否则返回 dbTrue: public static DBBool operator &(DBBool x, DBBool y) { return new DBBool(x.value < y.value? x.value: y.value); } // 逻辑 OR 运算符。如果任何一个操作数为 // dbTrue,则返回 dbTrue,如果任何一个操作数为 dbNull,则返回 dbNull,否则返回 dbFalse: public static DBBool operator |(DBBool x, DBBool y) { return new DBBool(x.value > y.value? x.value: y.value); } // 绝对真运算符。如果操作数为 // dbTrue,则返回 true,否则返回 false: public static bool operator true(DBBool x) { return x.value > 0; } // 绝对假运算符。如果操作数为 // dbFalse,则返回 true,否则返回 false: public static bool operator false(DBBool x) { return x.value < 0; } // 重载从 DBBool 到 string 的转换: public static implicit operator string(DBBool x) { return x.value > 0 ? "dbTrue" : x.value < 0 ? "dbFalse" : "dbNull"; } // 重写 Object.Equals(object o) 方法: public override bool Equals(object o) { try { return (bool) (this == (DBBool) o); } catch { return false; } } // 重写 Object.GetHashCode() 方法: public override int GetHashCode() { return value; } // 重写 ToString 方法以便将 DBBool 转换为 string: public override string ToString() { switch (value) { case -1: return "DBBool.False"; case 0: return "DBBool.Null"; case 1: return "DBBool.True"; default: throw new InvalidOperationException(); } } } class Test { static void Main() { DBBool a, b; a = DBBool.dbTrue; b = DBBool.dbNull; Console.WriteLine( "!{0} = {1}", a, !a); Console.WriteLine( "!{0} = {1}", b, !b); Console.WriteLine( "{0} & {1} = {2}", a, b, a & b); Console.WriteLine( "{0} | {1} = {2}", a, b, a | b); // 调用真运算符以确定 DBBool 变量的 // 布尔值: if (b) Console.WriteLine("b is definitely true"); else Console.WriteLine("b is not definitely true"); } }转载地址:http://ttgpf.baihongyu.com/