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.
In order to be applicable as a short circuit operator a user-defined logical operator ('operator') must have the same return type as the type of its 2 parameters.
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 parameters and return values of the same type. For more information on short-circuit operators, see && Operator and || Operator.
The following sample generates CS0217:
// CS0217.cs
using System;
public class MyClass
{
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 int operator & (MyClass f1, MyClass f2) // CS0217
// try the following line instead
// public static MyClass operator & (MyClass f1, MyClass f2)
{
return new MyClass();
}
public static void Main()
{
MyClass f = new MyClass();
int i = f && f;
}
}