Tech Off Post

Single Post Permalink

View Thread: Ideas for C# 5.0
  • User profile image

    BitFlipper said:
    exoteric said:

    Well, if you look at anonymous methods and captured variables etc, the compiler already creates anonymous classes in that case as you mention. We don't really care too much about the signature of these classes, since they are mostly hidden from us (although you can certainly see them in the debugger). In addition, when you use IEnumerable and yield return, the compiler infers the type from the return value. So if you combine those two concepts, you can basically have tuples with named items, right?


    So in my example, the type of "result", would be something like "<Tuple>a__1" or whatever, but it would contain the public members with the correct names and types. Edit: And this is also what you would see if you used reflection to query the enclosing class's properties.


    BTW, in my example then it should really be returned like this:


    public ITuple Result
        get { return (Count = runCount, Min = runMin, Max = runMax, Avg = runAvg); } 


    So just like you use IEnumerable when using yield return, you use ITuple, and the return type determines its true signature.



    This is very similar to anonymous types:


    var x = new { Count = runCount, Min = runMin, Max = runMax, Avg = runAvg };


    The only difference is that anonymous types can only be used in a local scope.  I think that you should be able to return them from methods.  It is still just as type-safe but a lot more succinct:


    public var GetAnonymous() {
      return new { Count = runCount, Min = runMin, Max = runMax, Avg = runAvg };