C# Error CS0837 – The first operand of an ‘is’ or ‘as’ operator may not be a lambda expression, anonymous method, or method group.

C# Error

CS0837 – The first operand of an ‘is’ or ‘as’ operator may not be a lambda expression, anonymous method, or method group.

Reason for the Error & Solution

The first operand of an ‘is’ or ‘as’ operator may not be a lambda expression, anonymous method, or method group.

Lambda expressions, anonymous methods and method groups may not be used on the left side of or .

To correct this error

  • If the error involves the is operator, remember that is takes a value and a type and tells you whether the value can be made into that type by a reference, boxing, or unboxing conversion. Because lambdas are not values and have no reference, boxing, or unboxing conversions, lambdas are not candidates for is.

  • If the code misuses as, the correction is probably to change it to a cast.

Example

The following example generates CS0837:

// cs0837.cs
namespace TestNamespace
{
    public delegate void Del();

    class Test
    {
        static int Main()
        {
            bool b1 = (() => { }) is Del;   // CS0837
            bool b2 = delegate() { } is Del;// CS0837
            Del d1 = () => { } as Del;      // CS0837  
            Del d2 = delegate() { } as Del; // CS0837
            return 1;
        }
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...