locked
What is the different between these 2 ways of adding items into list object in c# RRS feed

  • Question

  • User269846090 posted

    I tried to add items into list.

    So I done that by using these 2 ways: I wanna to know the difference between these 2 ways.

    1: List<Employee> objList = new List<Employee>(){

     new Employee { FirstName = "Ganesh", LastName = "S", Salary = 10000.00 },
     new Employee { FirstName = "Ramesh", LastName = "T", Salary = 20000.00 },
     new Employee { FirstName = "Suresh", LastName = "U", Salary = 30000.00 }
    };

    2: List<Employee> objList = new List<Employee>();

    objList.Add(new Employee()
    {
    FirstName = "a",
    LastName = "aa",
    Salary = 1.00,

    });

    Wednesday, July 3, 2013 3:13 AM

Answers

  • User281315223 posted

    There really isn't any major difference in terms of syntax between these two (aside from the actual content that you are using within each of the lists).

    Your first approach simply instantiates the List of Employee objects with three Employees : 

    //This will create a List of Employees that contains the following 3 Employee objects
    List<Employee> objList = new List<Employee>(){
     new Employee { FirstName = "Ganesh", LastName = "S", Salary = 10000.00 },
     new Employee { FirstName = "Ramesh", LastName = "T", Salary = 20000.00 },
     new Employee { FirstName = "Suresh", LastName = "U", Salary = 30000.00 }
    };

    Where as your second implementation will create an empty list and add an Employee to the empty list using the List.Add() method

    //This will create an empty List of Employee objects
    List<Employee> objList = new List<Employee>();
    
    //Manually add each of the objects
    objList.Add(new Employee { FirstName = "Ganesh", LastName = "S", Salary = 10000.00 }); 
    objList.Add(new Employee { FirstName = "Ramesh", LastName = "T", Salary = 20000.00 }); 
    objList.Add(new Employee { FirstName = "Suresh", LastName = "U", Salary = 30000.00 }); 
    

    They are basically both accomplishing the same thing just with different syntax.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 4, 2013 11:39 AM
  • User-1910946339 posted

    Rion Williams

    Your first approach simply instantiates the List of Employee objects with three Employees : 

    Rion Williams

    Where as your second implementation will create an empty list and add an Employee to the empty list using the List.Add() method

    Actually they both create an empty list by calling the parameterless constructor and then they both use the Add method to add each of the elements.

    The difference is that in the first case the empty list is created as a temporary variable and then List.Add is used to add the new elements to that temporary variable.  Once all the elements are added the temporary variable is saved to the actual variable (objList).

    In the second case, objList is created as an empty list by calling the same parameterless constructor.  Then the elements are added to objList.  There is no temporary variable and no final save.

    The difference in behaviour which could be seen is a by-product of the small creation difference.  In the second case another thread could read objList immediately after construction and before the call to Add and see that objList has no elements.  In the first case this is not possible - as far as another thread is concerned the final objList just 'pops' into existance, fully formed with all its elements, there is no visible intermediate stage where the list is partially populated.

    <Re-reading Rion's post I can see that it could be interpreted in the way that I have expanded, I just thought it worth being very precise about the actual sequence of actions>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 4, 2013 7:02 PM

All replies

  • User527778624 posted

    Hi,

    First approach is for coding convience using latest .Net version (3.5+)

    There is no diff. between those two.

    Check this site for details:

    http://msdn.microsoft.com/en-us/library/vstudio/bb384062.aspx

    Wednesday, July 3, 2013 3:22 AM
  • User-1910946339 posted

    Unless this is a trick question (the first list has three elements and the second has one element) then there is no difference.

    Grab yourself a copy of linqpad from www.linqpad.net and type the code in yourself.  The IL tab will show you the compilation result and you will be able to see if there are any differences.

    Wednesday, July 3, 2013 6:43 PM
  • User-1002157272 posted

    The difference between the two appoaches is that approach #1 is using what is called initialization syntax. The Employee objects are being added to the List<T> object during it's initialization. Approach #2 is first initializing the Lits<T> and then adding an Employee object after the List<T> has been initialized. Just for clarification, the Employee object itself is using initialization syntax in approach #2. Is this the answer you were looking for?

    Thursday, July 4, 2013 11:31 AM
  • User281315223 posted

    There really isn't any major difference in terms of syntax between these two (aside from the actual content that you are using within each of the lists).

    Your first approach simply instantiates the List of Employee objects with three Employees : 

    //This will create a List of Employees that contains the following 3 Employee objects
    List<Employee> objList = new List<Employee>(){
     new Employee { FirstName = "Ganesh", LastName = "S", Salary = 10000.00 },
     new Employee { FirstName = "Ramesh", LastName = "T", Salary = 20000.00 },
     new Employee { FirstName = "Suresh", LastName = "U", Salary = 30000.00 }
    };

    Where as your second implementation will create an empty list and add an Employee to the empty list using the List.Add() method

    //This will create an empty List of Employee objects
    List<Employee> objList = new List<Employee>();
    
    //Manually add each of the objects
    objList.Add(new Employee { FirstName = "Ganesh", LastName = "S", Salary = 10000.00 }); 
    objList.Add(new Employee { FirstName = "Ramesh", LastName = "T", Salary = 20000.00 }); 
    objList.Add(new Employee { FirstName = "Suresh", LastName = "U", Salary = 30000.00 }); 
    

    They are basically both accomplishing the same thing just with different syntax.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 4, 2013 11:39 AM
  • User-1910946339 posted

    Rion Williams

    Your first approach simply instantiates the List of Employee objects with three Employees : 

    Rion Williams

    Where as your second implementation will create an empty list and add an Employee to the empty list using the List.Add() method

    Actually they both create an empty list by calling the parameterless constructor and then they both use the Add method to add each of the elements.

    The difference is that in the first case the empty list is created as a temporary variable and then List.Add is used to add the new elements to that temporary variable.  Once all the elements are added the temporary variable is saved to the actual variable (objList).

    In the second case, objList is created as an empty list by calling the same parameterless constructor.  Then the elements are added to objList.  There is no temporary variable and no final save.

    The difference in behaviour which could be seen is a by-product of the small creation difference.  In the second case another thread could read objList immediately after construction and before the call to Add and see that objList has no elements.  In the first case this is not possible - as far as another thread is concerned the final objList just 'pops' into existance, fully formed with all its elements, there is no visible intermediate stage where the list is partially populated.

    <Re-reading Rion's post I can see that it could be interpreted in the way that I have expanded, I just thought it worth being very precise about the actual sequence of actions>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 4, 2013 7:02 PM