简体   繁体   English

为什么不能在数组中交换匿名类型的属性?

[英]Why can't the properties of anonymous types be swapped within arrays?

I know that in C# I can declare an array of anonymous types like this: 我知道在C#中我可以声明一个匿名类型数组,如下所示:

var anons = new[]
{
    new { name = "" , something = ""},
    new { name = "", something = "" }
};

I can understand that all the objects need to have the same properties, or else one won't be able to iterate through them and use those properties like: 我可以理解所有对象都需要具有相同的属性,否则将无法遍历它们并使用以下属性:

foreach (var anon in anons)
{
    Console.WriteLine(anon.name);
}

But what I don't understand is why do their properties need to have the same order? 但我不明白为什么他们的房产需要有相同的订单?

For example, the following code won't compile: 例如,以下代码将无法编译:

var anons = new[]
{
    new { name = "" , something = ""},
    new { something = "", name = "" }
};

Why isn't this allowed, since in a normal object, the properties can be declared no matter their order, and the rest of the code could use them, as it does right now? 为什么不允许这样做,因为在普通对象中,无论顺序如何都可以声明属性,其余代码可以使用它们,就像现在一样?

From the documentation for anonymous types: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types 从匿名类型的文档: https//docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types

If two or more anonymous object initializers in an assembly specify a sequence of properties that are in the same order and that have the same names and types , the compiler treats the objects as instances of the same type. 如果程序集中的两个或多个匿名对象初始值设定项指定了具有相同顺序且具有相同名称和类型的属性序列 ,则编译器会将对象视为相同类型的实例。 They share the same compiler-generated type information. 它们共享相同的编译器生成的类型信息。

In other words, you're creating two anonymous types and the sequence of property names and property types don't match. 换句话说,您正在创建两个匿名类型,并且属性名称和属性类型的序列不匹配。 They look pretty much the same to us, but the compiler sees them as two different types, which means that it can't infer one type for the array declaration. 它们对我们看起来几乎相同,但编译器将它们视为两种不同的类型,这意味着它无法推断出数组声明的一种类型。

Even though the answer given by Scott is virtually correct, it does not answer the why question entirely. 尽管斯科特给出的答案几乎是正确的,但它并没有完全回答原因 Since this is a question more about the compiler design regarding anonymous types array, and most of us can't come up with a clear answer, I will keep the question open and postpone accepting an answer until somebody answers the why question. 由于这是关于匿名类型数组的编译器设计的更多问题,并且我们大多数人都无法得出明确的答案,我将保持问题公开并推迟接受答案,直到有人回答原因问题为止。 Meanwhile I will leave here the following theory: 同时我将留下以下理论:

To the question 对于这个问题

why do their properties need to have the same order? 为什么他们的房产需要有相同的订单?

Why not? 为什么不?

Since one can declare an array of anonymous types right now, having the possibility to swap the order of the properties within objects would not help anybody. 因为现在可以声明一个匿名类型的数组,有可能交换对象中属性的顺序对任何人都没有帮助。

Contrarily to this, changing the order of the properties of an anonymous object within the objects of an array of anonymous types can be misleading and for the sake of consistency should be avoided. 与此相反,在匿名类型的数组的对象内改变匿名对象的属性的顺序可能会产生误导,并且为了保持一致性应该避免。 In this case, a warning or a at least a message should be displayed for the programmers who are trying to swap the properties of the objects like that so why not making it an error from the beginning and remove the need to make the C# compiler smart enough to sort out the properties itself. 在这种情况下,应该为正在尝试交换对象属性的程序员显示警告或至少一条消息,以便为什么不从头开始使其成为错误,并且无需使C#编译器变得智能足以理清属性本身。

In conclusion: Having the properties always in the same order is not a missing functionality, but a feature. 总之:使属性始终处于相同的顺序不是缺少功能,而是功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM