I am getting a rather strange error:
CS8121 An expression of type 'IGrouping<string, IMyInterface>' cannot be handled by a pattern of type 'MyClass1'.
The error is occuring on the line:
if(tGroup is MyClass1 myclass1)
However, no error is provided for MyClass2 which is not sealed.
- What is causing this?
- Other than not sealing
MyClass1what solutions are there?
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo
{
class Program
{
static void Main()
{
IEnumerable<IMyInterface> myInterfaces = new List<IMyInterface>();
foreach (IGrouping<String, IMyInterface> tGroup in myInterfaces.GroupBy(x => x.XXX))
{
if(tGroup is MyClass1 myclass1)
{
}
if(tGroup is MyClass2 myClass2)
{
}
}
}
}
public interface IMyInterface
{
String XXX { get; }
}
public sealed class MyClass1 : IMyInterface
{
public String XXX { get; }
}
public class MyClass2 : IMyInterface
{
public String XXX { get; }
}
}