Redundant explicit property name in C# warning

I am using the SimpleMembership provider and adding additional fields to be saved on registration.

To save additional fields on registration I use the method used everywhere e.g. Link

I use the following SimpleMembership provider method called in my HttpPost ActionResult Register method

 WebSecurity.CreateUserAndAccount(registerModel.UserName, registerModel.Password,
    new {  FirstName = registerModel.FirstName, LastName = registerModel.LastName, CompanyName = registerModel.CompanyName});

Additional fields get added by a by value method parameter of type object. the description for this is "A dictionary that contains additional user attributes"

So this works.

My question is I have Jetbrains Resharper 8 installed and for the property names I get "redundant explicit property name" warnings. To remove the warnings it I change my code will look like:

 WebSecurity.CreateUserAndAccount(registerModel.UserName, registerModel.Password,
    new {  registerModel.FirstName, registerModel.LastName, registerModel.CompanyName}); 

My question are 1) for this anonymous object parameter where I now remove the property names so I don't get the Resharper warnings anymore, how does it know what the property names will be use to match to the db property class since they are passed by value?

2) what is best practice having the property names in or not. Having them not in is not very readable and in code samples they are specified so I would imagine having them in is better for readability rather than removing it

Jon Skeet
people
quotationmark

My question are 1) for this anonymous object parameter where I now remove the property names so I don't get the Resharper warnings anymore, how does it know what the property names are since they are passed by value?

The names are inferred (by the compiler) from the expressions. This only works when you're using a field or property (not a literal or a method for example). This is in section 7.6.10 of the C# language specification.

2) what is best practice having the property names in or not. Having them not in is not very readable and in code samples they are specified so I would imagine having them in is better rather than removing it

If it's more readable for you and your team to always include the name, then go ahead and do so. For other people, it may feel a little redundant. Just adjust your R# settings to fit your local conventions.

Your argument that "in code samples they are specified" is specious though - there are some examples where they're specified and some where they're not. I suspect you'll find a mixture even within particular example providers (e.g. in MSDN).

people

See more on this question at Stackoverflow