Which one of these is the faster/better one?
This one:
List list = new List();
User u;
foreach (string s in l)
{
u = new
In any case, the best way would be to use a constructor that takes a Name... or, otherwise, exploit curly-brace notation:
foreach (string s in l)
{
list.Add(new User(s));
}
or
foreach (string s in l)
{
list.Add(new User() { Name = s });
}
or even better, LINQ:
var list = l.Select( s => new User { Name = s});
Now, while your first example could, in some cases, be unperceptibly faster, the second one is better because it's more readable, and the compiler may discard the variable (and omit it altogether) since it's not used outsid the foreach's scope.