編譯器錯誤 CS0218
更新:2007 年 11 月
錯誤訊息
型別 ('type') 必須包含運算子 true 和運算子 false 的宣告
如果您定義了一個使用者定義型別的運算子,然後嘗試將該運算子當成最少運算 (Short Circuit) 運算子使用,則該使用者定義運算子必須定義運算子 true 和運算子 false。如需最少運算子的詳細資訊,請參閱 && 運算子和 || 運算子。
下列範例會產生 CS0218:
// CS0218.cs
using System;
public class MyClass
{
// uncomment these operator declarations to resolve this CS0218
/*
public static bool operator true (MyClass f)
{
return false;
}
public static bool operator false (MyClass f)
{
return false;
}
*/
public static implicit operator int(MyClass x)
{
return 0;
}
public static MyClass operator & (MyClass f1, MyClass f2)
{
return new MyClass();
}
public static void Main()
{
MyClass f = new MyClass();
int i = f && f; // CS0218, requires operators true and false
}
}