Compiler Error CS0217
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 about short-circuit operators, see &&
operator and ||
operator. For more information about user-defined short-circuit, or conditional, operators, see the User-defined conditional logical operators section of the C# language specification.
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;
}
}
.NET feedback
.NET is an open source project. Select a link to provide feedback: