7

C# is mostly stronglt typed, however, there are a few odd behaviors:

e.g. when rolling your own enumerator class, you do not have to implement IEnumerable, you only have to implement a method called GetEnuerator(), you will still be able to foreachover it.

Same goes for some of the Linq support, you can write your own Linq provider just by providing some select where methods etc.

So why is IDisposable required for the using(x){} statement?

I do get why the linq provider can be written in such a way since there is no fixed contract to fullfill, only a sort of pattern of methods.

Why was the C# compiler(or language spec?) designed in such a way that interfaces are needed for some of the built in language constructs, but not for others?

Roger Johansson
  • 22,764
  • 18
  • 97
  • 193
  • 4
    Eric Lippert explains it here: http://blogs.msdn.com/b/ericlippert/archive/2011/06/30/following-the-pattern.aspx – John Koerner Jan 13 '13 at 12:41
  • Re "weakly typed": http://stackoverflow.com/a/9929697/395760 (plus, none of the numerous definitions of weak typing I know would cover this: It *is* type-checked statically, just structurally instead of nominally.) –  Jan 13 '13 at 12:41
  • So, `foreach` is designed the way it is because .NET did not have generics back when it first came out. interesting :-) – Roger Johansson Jan 13 '13 at 12:47
  • 2
    possible duplicate of [Duck typing in the C# compiler](http://stackoverflow.com/questions/6368967/duck-typing-in-the-c-sharp-compiler) – John Koerner Jan 13 '13 at 12:54
  • @JohnKoerner yes, duplicate. wonder why SO didn't show that question as a hint when naming this one.. odd – Roger Johansson Jan 13 '13 at 12:57

0 Answers0