I'm new to c# and I'm totally against var
.
Every time when I see var
in code example, it makes me crazy, "what type is it?" even the semantic is obvious, I'd like to see the full type name. The presence of full type name makes me feel that the code is elegant and professional.
I know var
can be necessary if using an anonymous type, but I'm against anonymous type too. If you want to use a type, why not give it a name? The following is some example of using var
:
var results = context.People.Select(p => new {p.PersonID, p.Name});
But I think it should be written like this: (Don't argue about the definition or syntax, I'm not sure whether it's correct because I'm not familiar with linq, I just want to say "why not give it a name?")
class Result
{
public int PersonID;
public string Name;
public Result(int id, string name)
{
PersonID = id;
Name = name;
}
}
Result results = context.People.Select(p => new Result(p.PersonID, p.Name));