Duck typing in the C# compiler

后端 未结 3 1434
囚心锁ツ
囚心锁ツ 2020-11-27 19:06

Note This is not a question about how to implement or emulate duck typing in C#...

For several years I was under the impression that certai

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 20:04

    The question which you are asking is not a chicken and egg situation. Its more like hows the language compiler is implemented. Like C# and VB.NET compiler are implemented differently.If you write a simple code of hello world and compile it with both the compiler and inspect the IL code they will be different. Coming back to your question, I will like to explain what IL code is generated by C# compiler for IEnumerable.

    IEnumerator e = arr.GetEnumerator();
    
    while(e.MoveNext())
    {
       e.Currrent;
    }
    

    So the C# compiler is tweaked for the case of foreach.

提交回复
热议问题