2011年4月7日 星期四

[轉]運算符資料

☆C#的運算符定義只有四種形式:---------------------------------------
1public static 返回類型 operator ?(單形參)
2public static 返回類型 operator ?(雙形參)
3public static implicit operator 隱轉目標類型(單源類型形參)
4public static explicit operator 顯轉目標類型(單源類型形參)
△全部都是傳值,故不可用ref和out參數
△全部都是static
△比較運算符的重載必須貫徹成對一起定義的原則來實現C#的一站式編碼
△true()和false()也是運算符,分別表示 非(假/空)嗎 和 非(真/空)嗎
☆可重載的運算符以及解決方法可行性表(摘自vs.n中文正式版幫助):--------------------------------
運算符 可重載性
+, -, !, ~, ++, --, true(), false() 可以重載這些一元運算符。
+, -, *, /, %, &, | , ^, <<, >> 可以重載這些二進制運算符。
==, !=, <, >, <=, >=            可以重載這些比較運算符(但必須成對重載)。
&&, ||                                 不能重載條件邏輯運算符,但可使用 & 和 | 對其進行計算,可以重載 & 和 |;請參閱 7.11.2 用戶定義的條件邏輯運算符。
[]                                       不能重載數組索引運算符,但可定義索引器。
()                                      不能重載轉換運算符,但可定義新的轉換運算符(請參閱 explicit 和 implicit)。
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
                                        不能重載賦值運算符,但例外的是可使用 +(該運算符可被重載)計算 +=。
=, ., ?:, ->, New, is, sizeof(), TypeOf()
                                        不能重載這些運算符。


    ☆一些散題:-------------------------------------
1傳遞對像引用是傳值的一種,不屬於傳址.因為對象的值只是實體的地址,如果傳對像變量的地址才算是傳址
2readonly比const寬鬆,因為也可以在構造器中賦值,此外【static readonly】<=>【const】
3using簡化書寫的其中一個功能就是創建【姓或類型】的別名,可減少修改
4using(IDispose化的對象【,....】){ }語句可以確保句尾調用對像.Dispose()
5代碼和註釋是最好的心得!

☆以下是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");  
   }
}

沒有留言:

張貼留言