views:

159

answers:

2

I see language features like C#'s foreach loop (which requires the use of IEnumerable), and its using syntax (which uses IDisposable, knowing which method to call), and LINQ (which I assume uses a heap of .NET stuff too). All of this seems very tied-in to the .NET framework. So:

  • Could C# exist with a different framework? (Does it already?)
  • If so, how does a language specify "foreach loops require an IEnumerable instance" without tying it in to a framework.
+3  A: 
  1. Yes - Mono is an open source cross-platform implementation of the .Net framework.
  2. Not sure, good question. Mono replaces both the .Net framework and the CLR, and so this is not a problem for Mono.
Kragen
True, although, as you say, Mono is an implementation of the .NET framework. It's not really a different framework. I'm more wondering along the lines of "Could C# work with something so different as the Python library?"
Smashery
+6  A: 

The best place to investigate this is the C# language specification. For example, in the case of enumerables, here are some snippets from the spec:

10.14.2 Enumerable interfaces

The enumerable interfaces are the non-generic interface System.Collections.IEnumerable and all instantiations of the generic interface System.Collections.Generic.IEnumerable. For the sake of brevity, in this chapter these interfaces are referenced as IEnumerable and IEnumerable, respectively.

...

10.14.5.1 The GetEnumerator method

An enumerable object provides an implementation of the GetEnumerator methods of the IEnumerable and IEnumerable interfaces. The two GetEnumerator methods share a common implementation that acquires and returns an available enumerator object. The enumerator object is initialized with the argument values and instance value saved when the enumerable object was initialized, but otherwise the enumerator object functions as described in §10.14.4.

This ties in nicely with how these interfaces are actually defined in the .NET BCL, although it would be completely possible for a different (i.e. non-.NET) framework to provide a similar, compatible implementation that a C# compiler could then target.

Portions of the spec that are hard to separate from the .NET framework are normally marked with:

Note: This section is applicable only to the Microsoft .NET implementation of C#.

On another note,foreachdoesn't actually requireIEnumerable; it can use a sort of 'duck-typing', which you can read about in section 8.8.4 of the spec.

Ani
Ah, so it's really all about the *implementation* of the language, then?
Smashery
@Smashery: I think it's better to think of it in terms of the implementation of a compliant *compiler*.
Ani
As an aside, LINQ *syntax* doesn't place any specific demands either; like `foreach`, it is entirely pattern-based. Expression trees are more dependent, but the details of this translation is actually not in the spec anyway.
Marc Gravell