C# quiz answer – yes it compiles, and here’s why

Got a few good comments to the quiz post, but nobody that commented got it right.  I went ahead and sent the quiz on an internal C# mailing list, and to nobody’s surprise, multiple people got it right 🙂 The key bit of information is that the one thing you can add to the Class2…

1

Today’s C# quiz – does this compile?

using System; using System.Collections.Generic; class Program { static void Main(string[] args) { IEnumerable<Inter1> ienum1 = new Class1[] { new Class1() }; foreach (Class2 c in ienum1) { Console.WriteLine(c); } } } public interface Inter1 { } public class Class1 : Inter1 { } public class Class2 { }

12