Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# foreach unexpected behavior

Tags:

c#

covariance

Why C# compiler allows this to compile and throws runtime exception when run?

class Program
{
   static void Main(string[] args)
   {
      IEnumerable<Test> list = new List<Test>() { new Test() };

      foreach(IDisposable item in list)
      {

      }
   }
}

public class Test
{

}

This does compile with any interface and it doesn't compile if you replace IDisposable with concrete class.

like image 271
ekalchev Avatar asked Dec 18 '19 15:12

ekalchev


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

The foreach loop has an implicit cast in it. It's roughly like this:

using (IEnumerator<Test> iterator = list.GetEnumerator())
{
    while (iterator.MoveNext())
    {
        IDisposable item = (IDisposable) iterator.Current;
        // Body of foreach loop here
    }
}

Back before generics, that was much handier than having to cast in the source code. Now it's not so important, but it would be odd for it to not compile. Note that the compiler will check that it's at least feasible. If you use foreach (string item in list) that wouldn't compile, because a Test can't be a string - but a Test can be an IDisposable, because it could refer to an instance of a subclass of Test that implements IDisposable. If you make the Test class sealed, it will fail to compile even with IDisposable, too, because then a Test instance can't implement IDisposable.

Basically, it will compile if a cast from Test to the iteration type would compile, and will fail to compile otherwise. But it'll fail at execution time if a normal cast would fail at execution time, too.

like image 85
Jon Skeet Avatar answered Oct 05 '22 15:10

Jon Skeet