Expression of type T cannot be handled by a pattern of type X

11,892

Solution 1

As explained by Neal Gafter from Microsoft:

The reason it doesn’t work is that there is no conversion (explicit or implicit) defined from T to KeepalivePacket. Pattern matching requires such a conversion to exist, as it is defined in terms of the cast operator, which requires a conversion exist. The language specification and compiler agree that no conversion exists. It seems strange to me that the language specification is defined such that no (explicit) conversion exists here. We'll look at what we can do about that.

We're not going to do anything about this in C# 7. You'll have to add a cast to your code to work around it. Once we have recursive patterns, this may be more difficult to work around. Moreover, the awkward language rule that underlies this issue (i.e. that there is no conversion from T to KeepalivePacket) doesn't make a lot of sense.

Update

This is now working in C# 7.1

Solution 2

C# 7.1 supports this now. For example, see "Pattern Matching with Generics" in this article. You may need to add <LangVersion>7.1</LangVersion> or <LangVersion>latest</LangVersion> to your project file. See here for details on configuring LangVersion.

Solution 3

The answer to C#7.0 is

if ((Packet)packet is KeepalivePacket keepalive)
{
    // Do stuff with keepalive
}

switch ((Packet)packet)
{
    case KeepalivePacket keepalivePacket:
        // Do stuff with keepalivePacket
        break;
}
Share:
11,892
Alex Wiese
Author by

Alex Wiese

Updated on June 05, 2022

Comments

  • Alex Wiese
    Alex Wiese almost 2 years

    I have upgraded my project to target C# 7 and used Visual Studio 2017 RC to implement pattern matching across my solution. After doing this some errors were introduced relating to pattern matching with generic parameters.

    Consider the following code:

    public class Packet
    {
    }
    
    public class KeepalivePacket : Packet
    {
    }
    
    public void Send<T>(T packet)
        where T : Packet
    {
        if (packet is KeepalivePacket keepalive)
        {
            // Do stuff with keepalive
        }
    
        switch (packet)
        {
            case KeepalivePacket keepalivePacket:
                // Do stuff with keepalivePacket
                break;
        }
    }
    

    Both the if statement and the case statement produce a compilation error.

    An expression of type T cannot be handled by a pattern of type KeepalivePacket

    If I first cast the parameter to type object the pattern matching works as expected. Roslyn then marks the cast to object as redundant.

    if ((object)packet is KeepalivePacket keepalive)
    {
        // This works
    }
    

    This error only appears to apply to generic parameters and variables. Roslyn appears to not be aware of this issue as it recommends changing the code to use pattern matching via an analyzer and allows me to apply the "code fix" resulting in the broken code.

  • CervEd
    CervEd over 3 years
    not sure why this got downvoted, if you're stuck on C#7.0 this is how you have to solve it. If you're able to use a more modern C# version then by all means use pattern matching like you would in a normal language