Declaring and creating an object then adding to collection VS Adding object to collection using new

Posted by ZeeMan on Stack Overflow See other posts from Stack Overflow or by ZeeMan
Published on 2010-03-27T00:03:22Z Indexed on 2010/03/27 0:13 UTC
Read the original article Hit count: 188

Filed under:

Ok so the title may have been confusing so i have posted 2 code snippets to illustrate what i mean.

NOTE: allUsers is just a collection.

RegularUser regUser = new RegularUser(userName, password, name, emailAddress);
        allUsers.Add(regUser);

VS

        allUsers.Add(new RegularUser(userName, password, name, emailAddress));
  1. Which snippet A or B is better and why?
  2. What are the advantages or disadvantages?
  3. The example i wrote was C# but does the language (C#, Java etc) make a difference?

© Stack Overflow or respective owner

Related posts about c#