☆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");
}
}
網站運營seo文章大全提供全面的站長運營經驗及seo技術!