Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The type ('type') must contain declarations of operator true and operator false
If you define an operator for a user-defined type, and then try to use the operator as a short-circuit operator, the user-defined operator must have operator true and operator false defined. For more information on short-circuit operators, see && Operator and || Operator.
The following sample generates 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
}
}