Anonymous type optimization #8014
Replies: 9 comments 21 replies
-
I'm not clear on what you're proposing. The title of the issue includes "optimization"... what is that referring to? Are you instead suggesting a new syntax to create named types without declaring them? |
Beta Was this translation helpful? Give feedback.
-
Anonymous types are artifacts of compilers. The runtime has no concept of it. This should be a discussion to csharplang repo. |
Beta Was this translation helpful? Give feedback.
-
I wish there were new syntax to make anonymous types easier to use |
Beta Was this translation helpful? Give feedback.
-
This is the simplest application scenario, and there are many, many more var list = db.Orders
.Select(it => new { Id = 1,Name=o.Name, CustomName = cus.Name })
.ToList();
//error
list.Add(new { Id = 1,Name=”a“, CustomName = "aa" }) If supported, it can be compiled by pointing to the same type var list = db.Orders
.Select(it => new (name) { Id = 1,Name=o.Name, CustomName = cus.Name })
.ToList();
list.Add(new (name) { Id = 1,Name=”a“, CustomName = "aa" })
Console.WriteLine( new (name){ name = 1 }.GetType().FullName);
//<>f__AnonymousType61`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Console.WriteLine(new(name) { name = 1 }.GetType().FullName);
//<>f__AnonymousType61`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] Point to the same name without changing the original name |
Beta Was this translation helpful? Give feedback.
-
Maybe I sent it to the wrong place, but I just wanted to pass on the idea |
Beta Was this translation helpful? Give feedback.
-
If you want a name, declare a type. Once it has a name it's no longer anonymous. Also, anonymous types are readonly, so creating one to mutate later is not something that would work anyway. Anonymous types are not intended to replace declaring proper types, they're intentionally opinionated because they're intended for projections. |
Beta Was this translation helpful? Give feedback.
-
The only thing I can think of that can be improved here is declaring method-scoped record types, especially record structs. I'm not sure if there is an existing thread for it. Even that idea is likely to meet objections like "is it really needed" and the new complexity. |
Beta Was this translation helpful? Give feedback.
-
Really need this feature, a lot of application scenarios. The cost of creating a class is too high, the tuple is not too easy to maintain, and the anonymity only needs to be identified as the same anonymous type, so that it can be reused |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Anonymous classes have no way to reuse the effect is very discounted
The name simply identifies them as the same type, so that it can be repeated
Beta Was this translation helpful? Give feedback.
All reactions