Anonymous Structs

Is c# 3.0 going to support anonymous structs ever. like:

struct{int;int;} bgHome = new {42,21};

If not why not It seems like a great alternative to always goin the var route. If you are going to support it, will you also provide support for using them as return types.



Answer this question

Anonymous Structs

  • Sephers

    You should check out comega to see this functionality at work. The answer is:

    MyTextBox.Text = person[0];

    unnamed fields are accessed by their index.


  • Johnny37

    Assuming you have an anonymous struct (or class which is prefered for boxing and other performance reasons) as you defined as

    class {int, string} person = new {28, "Jerod Moemeka"};

    and returned a class as this anonymous type, in your calling class, how would you get the value 28 out of the person class. In other words, what would you use to replace the in the example below:

    MyTextBox.Text = person.

    Jim Wooley
    http://devauthority.com/blogs/jwooley/default.aspx



  • Sumathi

    Giving us this ability allows us to expicitly define our anonymous types upfront, hence we can pass them and arguments and return them from methods unlike var. I guess I dont understand why this was not provided before var, since this approach is much more inline with strong typing that var is.


  • JoeJeff

    well coulnt the type name be int_string_type rather than projection_<####> or whatever is used now
  • Philippe Trottier

    var is strongly typed.  The type in question is just inferred at compile time from the assignment on the RHS, rather than design-time or runtime.  That is,

        var foo = new FooType();

    and

        FooType foo = new FooType();

    are the same.

    The problem is different from anonymous types in that, not knowing the name of the type at design time (beyond the fact that like all other types, it inherits from object), you can't refer to the when creating the message signature.  That's the "anonymous" part, and why var is needed for variable declaration.

     



  • shadow2026

    Interesting idea... This reminds me of using tuples in ML. Would this require adding new functionality to the .NET type system, or would you envision handling this in the compiler somehow

  • abi

    I understand the significance of var, my question is, why not also allow for explicitly declaring the structure of an anonymous type instead of using var. For instance:

    class {int, string} or class {int Age,string Name}

    although var solves the immediate problem of being a placeholder for any type that may be constructed dynamically, it falls short of solving the underlying problem. I need a way to utilize and persist by dynamically created types in other parts of my code. This shortcomming is exposed in all the query samples MSFT provides: Every linq query is immediately followed by a foreach statement. I really think the pattern is missing that last bit of functionality.


  • c#re_eye

    The design is that anonymous types are classes.

  • KFischer

    so will we be able to do this:

    class {int,string} person = new {28,"Jerod Moemeka"};

    or will it always be like:

    var person = new {Age=28, Name="Jerod Moemeka"};

    Although I appreciate the ease of use of var, I would love to see that functionality beyond method scope. It would be great to be able to return these dynamic types or pass them in as arguments into methods. I would like to know if this type of functionality will be available, if not I'm interested in knowing what reasons pushed the C# linq team away from using it; since comega provides the functionlity.


  • aggieben

    You mean something like this (from before I joined Microsoft) http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=132589&SiteID=1

    If you're going to be sending things out of a method, you need some well-known interface to refer to. That's not much of a problem, I think. What I was thinking at the time was that it's a bear to have to implement the interface for little things like this.

    However, once you start on this path -- where the compiler or runtime automatically generates a type that conforms to an interface, using some basic type-building strategy -- there are interesting ideas, such as supplying some more complex strategy, so you can say "create IFoo, but use the DLinqEntityBuilder" or "... use the WCFBuilder", depending on your needs.



  • Anonymous Structs