Best practice when returning an array of values (.NET)

后端 未结 8 750
灰色年华
灰色年华 2021-01-14 14:37

Usually my methods are as the following:

public List Method1(int input)
{
    var output = new List();
    //add some items to output
          


        
相关标签:
8条回答
  • 2021-01-14 15:20

    It depends.

    Do you want the caller to be able to modify the items and for you to see those changes? Arrays can be modified. The IList interface defines methods for modification (but the implementation may not allow it).

    Can you elaborate on the FxCop warning?

    Kent

    0 讨论(0)
  • 2021-01-14 15:27

    I almost always go with List because it gives me methods that I frequently find the most useful.

    One potential negative consequence of returning an IEnumerable is that any exceptions that get thrown upon enumeration would come from an area of code that could be quite far away from the code that actually built the object, making bug tracking harder.

    0 讨论(0)
提交回复
热议问题