What is the best way to return a list<t> via a method?

57,431

Solution 1

If you are creating a new list in your buildNamesList method, I would just say:

var namesList = buildNamesList(); 

Solution 2

How to return the list really depends on what you are doing.

If you simply need the whole list in memory, your approach is fine, though the way I would call it would be:

public void someMethod(){
  List<String> namesList = buildNamesList();
}

There is no need to initialize the variable to an empty list that will get replaced immediately.

If the list is very large or you simply need to traverse it, you can use the yield keyword and change the return type of the function to IEnumerable<string>, though your current design doesn't fit this pattern.

Solution 3

Don't assign it your variable to new List<T>() only to immediately replace it with your return value.

Solution 4

You don't need to initialise it twice, just do:

public void someMethod(){
    List<String> namesList = buildNamesList();
}

Although if you're an FxCop lover, strictly speaking it's not good practice to have methods return concrete types (List in this instance), you should return the interface (IEnumerable)

Edit: For reference it's FxCop rule CA1059 :)

Share:
57,431
imaginationpluscode
Author by

imaginationpluscode

Updated on May 30, 2020

Comments

  • imaginationpluscode
    imaginationpluscode almost 4 years

    Suppose I have a method that returns a list of String names.

    public List<String> buildNamesList(){
       List<String> namesList = new List<String>();
       //add several names to the list
       return namesList;
    }
    

    Now if I have a method that needs that namesList would returning it like so be the best way?

    public void someMethod(){
      List<String> namesList = new List<String>();
      namesList = buildNamesList();
      //traverse namesList
    }
    
    • om471987
      om471987 almost 12 years
      Instead of return List<String> return IEnumerable<string>. IEnumerable is safe as its read only. You can convert it to list using ToList(). Also avoid String and use string. Another best practice
  • Andrei Drynov
    Andrei Drynov almost 12 years
    the best one for a lazy prorgammer like myself
  • Oded
    Oded almost 12 years
    Just to note that var is strongly typed to the return type of buildNamesList()
  • Dene B
    Dene B almost 12 years
    I would personally use implicit typing 'var' instead of 'List<string>' as it makes refactoring easier if you ever change the implementation. I would also have buildNamesList return an interface not a concrete class
  • Oded
    Oded almost 12 years
    @DeneB - For illustrative reasons (taking the level of the question), I decided not to use var in my example.
  • Dene B
    Dene B almost 12 years
    I got that, and I agree to the approach. :) I was just expanding for the sake of the OP.
  • imaginationpluscode
    imaginationpluscode almost 12 years
    I really like the simplicity! Thank you very much!