Why does a Linq Cast operation fail when I have an implicit cast defined?

前端 未结 4 1240
醉酒成梦
醉酒成梦 2020-11-27 06:39

I\'ve created two classes, with one of them having an implicit cast between them:

public class Class1
{
    public int Test1;
}

public class Class2
{
    pu         


        
4条回答
  •  情深已故
    2020-11-27 07:23

    A solution could be to use a bit of linq'ing here if you really need this kind of conversion:

    List items = new List{new Class2{Test2 = 9}};
    foreach (Class1 item in (from x in items select (Class1)x))
    {
        Console.WriteLine(item.Test1);
    }
    

提交回复
热议问题