Best practice: ref parameter or return value?

后端 未结 4 1207
悲哀的现实
悲哀的现实 2020-12-13 11:00

Actually I am doing a list as a reference parameter as follows:

public static List ListMethod(List result)

I saw some people doing in this

4条回答
  •  清歌不尽
    2020-12-13 12:00

    If you're just returning a List, you should always use the first one as it shows that intention.

    The version with ref tells me that I can start out with a list, and your method will modify the list I sent in, or even change it with another one. If that is your intention, do that.

    But if the method allways returns a new list, use a return value instead of a ref parameter.

    A note on the side: you could use out instead of ref to show your intentions of returning a new list, but this is only a good practice if you're using the return value for something else.

提交回复
热议问题