Home > Back-end >  Expression cannot be handled by pattern type
Expression cannot be handled by pattern type

Time:11-19

I am trying to make a switch statement to check against two types so I can count intersection points.

The code looks like this:

    public static int Intersects(this IGeometry object1, in IGeometry object2)
    {
        switch (object1, object2)
        {
            case Arc a1, Arc a2:
                    //get arc intersection count
                    break;

            default: return 0;
        }
        return 0;
    }

However I get the error:

An expression of type '(IGeometry object1, IGeometry object2)' cannot be handled by a pattern of type 'Arc'

Not sure why this is not possible because my Arc inherits it. My inheritance setup looks like this:

public struct Arc : ICurve

and

public interface ICurve : IGeometry

with

public interface IGeometry {}

How do I fix this error ? Not sure what I am getting wrong?

Thanks

CodePudding user response:

You are missing paranthesis:

public static int Intersects(this IGeometry object1, in IGeometry object2)
{
    switch (object1, object2)
    {
        case (Arc a1, Arc a2):
            //get arc intersection count
            break;

        default: return 0;
    }
    return 0;
}

You switch on a tuple, so the pattern has to be a tuple as well.

  •  Tags:  
  • c#
  • Related