dotnet / csharplang

The official repo for the design of the C# programming language
10.95k stars 999 forks source link

Anonymous type optimization #8013

Closed DotNetNext closed 2 months ago

DotNetNext commented 2 months ago

Anonymous classes have no way to reuse the effect is very discounted

//Want to support this
(name) x1 = new (name)
{
        id=1,
        name="a"
};

(name) x2=new (name);
x2.id=1;
x2.name="b";

The name simply identifies them as the same type, so that it can be repeated

stephentoub commented 2 months ago

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?

huoyaoyuan commented 2 months ago

Anonymous types are artifacts of compilers. The runtime has no concept of it. This should be a discussion to csharplang repo.

DotNetNext commented 2 months ago

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?

I wish there were new syntax to make anonymous types easier to use

DotNetNext commented 2 months ago

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" })
DotNetNext commented 2 months ago

Anonymous types are artifacts of compilers. The runtime has no concept of it. This should be a discussion to csharplang repo.

Maybe I sent it to the wrong place, but I just wanted to pass on the idea