D
D
DarkByte20152017-02-09 10:36:05
C++ / C#
DarkByte2015, 2017-02-09 10:36:05

Filtering with OfType?

Why doesn't this example output anything?

class Program
{
  static void Main(string[] args)
  {
    IEnumerable<IEnumerable<A>> a = new List<IEnumerable<A>> { new List<A> { new B(), new B() }, new List<A> { new C(), new C() } };
    var b = a.OfType<IEnumerable<B>>();
    var c = a.OfType<IEnumerable<C>>();

    foreach (var collections in b)
      foreach (var items in collections)
        items.foo();

    foreach (var collections in c)
      foreach (var items in collections)
        items.foo();
  }
}

abstract class A
{
  public abstract void foo();
}

class B : A
{
  public override void foo() => Console.WriteLine("B");
}

class C : A
{
  public override void foo() => Console.WriteLine("C");
}

PS IMHO the output should be: BBCC

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
leremin, 2017-02-09
@DarkByte2015

No, it shouldn't. You have a sheet of type A, and you check for B or C

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question