How much is the actual memory / performance gain in an ASP.NET MVC controller when using these two different ways of declaring the Model for a view?
User user = userService.GetByID(id);
return View(user);
or
return View(userService.GetById(id));
I’m supposing that the last one is a bit more performant as we do not initialize an object, however the first one is more readable. Would this even matter on a webserver with thousands of visitors?
Actually, you initialize an object in both cases; what the first is doing that the second is not is: 1) reserve some space for a variable (which doesn’t concern performance, but space); 2) add a reference to the object (that is, increment the number of references for that object) and then remove the reference (decrement the number of references) the row after.
I would hardly believe that a performance difference is observable between the two.
That said, I prefer the second, as there’s no need for the
uservariable, there; anyway, it’s just a matter of taste, there may be styling reasons to prefer the first and other ones to prefer the second.