Code inspection: Redundant anonymous type property explicit name
This inspection reports cases when property assignments in anonymous type initializers are explicitly declared, yet the compiler can unambiguously infer the property name from the variable or property being assigned.
When creating an anonymous type in C#, you can provide a new name for each property, or you can let the compiler automatically use the names from the expressions being assigned. For instance, if you write new { title }
, it implicitly creates an anonymous type with a property named title
based on the existing variable of the same name. However, if you explicitly write the property assignment as new { title = title}
, it would be redundant because the compiler will infer the property name from the variable.
In both cases (new { title }
or new { title = title}
) an identical anonymous type is produced. The inspection encourages the use of the more readable and concise implicitly named anonymous type initializers where possible.