C#重載操作符的那點事

來源:互聯網
上載者:User

如時實現C#操作符重載。

先寫關鍵詞public和static,後跟傳回型別,後跟operator關鍵詞,後跟要聲明的操作符符號,最後在對一對圓括弧中添加恰當的參數。

C#操作符重載方法:

1、編寫操作符重載方法。

2、執行個體化後,進行操作符運算

下邊用C#操作符重載代碼進行說明:

  internal struct Hour
{
private int Value;

public Hour(int ivalue)
{
this.Value = ivalue;
}
//定義個屬性用於取Value的值。
public int iValue {
get { return Value; }
set { Value = value; }
}
//聲明一個二元操作符,將兩個Hour相加
public static Hour operator +(Hour ihs, Hour rhs)
{
return new Hour(ihs.Value + rhs.Value);
}
/* 操作符是public的。所有操作符都必須是public的
操作符是static的。所有操作符都必須是static的,操作永遠不具有多態性,
面且不能使用virtual、abstract、override或者sealed修飾符。
二元操作符(比如+)有兩個顯式參數;一元操作符有一個顯式的參數
我們有了public Hour(int iValue)建構函式,就可以將一個int與Hour相加,
只是首先要將int轉換成為Hour hour a= ; int b= ; Hour sum=a+new Hour(b);
雖然上述代碼完全有效,但相較於讓一個Hour和一個int直接相加它即不清晰也不準確。
為了使Hour可以+ int,必須聲明一個二元操作符+,它的第一個參數是Hour,第二個參數是一個int。
C#操作符重載 */
public static Hour operator +(int ihs, Hour rhs)
{
return rhs + new Hour(ihs);//這裡調用上一個Hour的 (Hour+Hour)+ 重載
}
public static Hour operator +( Hour rhs,int ihs)
{
return ihs + rhs;//這裡調用上一個Hour的 (int,Hour) + 重載
}

/*C#中,下列操作符都是可以重載的:

+ - ! ~ ++ -- true false

* / % & | ^ << >> == != > < >= <=

但也有一些操作符是不允許進行重載的,如:

=,&&,||,?:,new,typeof,sizeof,is

一元操作符重載

顧名思義,一元操作符重載時操作符只作用於一個對象,此時參數表為空白,當前對象作為操作符的單運算元。
*/
public static Hour operator ++(Hour huValue)
{
huValue.iValue++; return huValue;
} //C#操作符重載
//==操作符是二元操作符,必須帶有兩個參數
public static bool operator==(Hour lhs,Hour rhs)
{
return lhs.iValue == rhs.iValue;
}
/*二元操作符重載

大多數情況下我們使用二元操作符重載。這時參數表中有一個參數,
當前對象作為該操作符的左運算元,參數作為操作符的右運算元。

下面我們給出二元操作符重載的一個簡單例子,即笛卡兒座標相加。*/
public static bool operator !=(Hour lhs, Hour rhs)
{
return lhs.iValue != rhs.iValue;
}

}
class Program
{
/*C#操作符:<、>;==、!=;true、false必須成對出現,即重載了“<”就必須重載“>”,重載了“==”就必須重載“!=”,重載了“true”就必須重載“false”;*/
static void Main()
{
Hour h = new Hour(30);
Hour p = new Hour(50);

Hour add = h + p;//(Hour+Hour)
add = h.iValue + p;//(int,Hour)
add = h + p.iValue;//(Hour ,int)
add++;//++(Hour)
Console.WriteLine(add.iValue);

Console.ReadKey();
}
//這個方法被jit編譯時間,BeforeFieldInit和Precise類的類型構造器還沒被執行,所以對這些構造器的調用將嵌入這個方法的代碼中,使它的運行變得較慢。
private static void PerfTest1(int iterations)
{
Stopwatch sw = Stopwatch.StartNew();
for (int x = 0; x < iterations; x++)
{
//jit編譯器最佳化調用BeforeFileldInit的類型構造器的代碼,使它在迴圈之前執行。
BeforefieldInit.s_x = 1;
}
Console.WriteLine("PerfTest1:{0} BeforeFileldInit", sw.Elapsed);
sw = Stopwatch.StartNew();
for (int x = 0; x < iterations; x++)
{
//jit編譯器在這裡產生調用Precise類的類型構造器的代碼,所以在每次迴圈迭代,都要核實一遍是否需要調用構造器。
Precise.s_x = 1;
}
Console.WriteLine("PerfTest1:{0} Precise", sw.Elapsed);
}
//這個方法被jit編譯時間,BeforeFieldInit和Precise類的類型構造器已經被執行,所以這個方法的代碼中,不會在產生對這些構造器的調用,它啟動並執行更快。

private static void PerfTest2(int iterations)
{
Stopwatch sw = Stopwatch.StartNew();
for (int x = 0; x < iterations; x++)
{
BeforefieldInit.s_x = 1;
}
Console.WriteLine("PerfTest2:{0} BeforeFileldInit", sw.Elapsed);
sw = Stopwatch.StartNew();
for (int x = 0; x < iterations; x++)
{
Precise.s_x = 1;
}
Console.WriteLine("PerfTest2:{0} Precise", sw.Elapsed);
}
}

C#程式設計語言要求操作符重載方法有一個參數的類型與當前定一個這個方法的類型相同。這樣的限制是為了使C#編譯器在合理的時間內找到要綁定的操作符方法。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.