简体   繁体   English

最佳实践:ref参数或返回值?

[英]Best practice: ref parameter or return value?

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 way too: 我看到有些人也这样做了:

public static void ListMethod(ref List result)

If I'm not wrong, "my" method also takes the list as reference parameter, and you should be able to use it just the same way as "other" does in his method. 如果我没有错,“my”方法也会将list作为参考参数,你应该能够像其他方法一样使用它。

But it seems more "clean" to me that you enter a parameter, do something with it and return it in the methods return value. 但是对我来说,输入参数,对它执行某些操作并在方法返回值中返回它似乎更“干净”。

Any good arguments for or against one method or the other? 对一种方法或另一种方法有任何好的论据吗?

It's likely that you don't need to use ref - but there is a difference. 可能是因为你不需要使用ref -但是有区别的。

Usually when I see people using ref for reference type parameters, it's because they don't understand how parameter passing works. 通常当我看到人们使用ref作为参考类型参数时,这是因为他们不了解参数传递的工作原理。 But if your method has something like this: 但是如果你的方法有这样的东西:

result = new List();
...

then in the first case the caller won't see the change, whereas in the second case the caller's variable will be changed to refer to the new object. 然后在第一种情况下,调用者将看不到更改,而在第二种情况下,调用者的变量将被更改为引用新对象。

See my article on parameter passing for a lot more detail. 有关详细信息,请参阅我关于参数传递的文章

No, your method does not use a ref parameter. 不,您的方法不使用ref参数。 The default is pass by value . 默认值是pass by value

The difference is, that your method can just modify the contents of your list but not the reference the parameter result points to. 不同的是,您的方法只能修改列表的内容,而不能修改参数result指向的引用。

Whats the best approach? 什么是最好的方法? It depends on what your method is supposed to do. 这取决于你的方法应该做什么。

When your method modifies the list or returns new data you should use the return value. 当您的方法修改列表或返回新数据时,您应该使用返回值。 Its much better to understand what your code does than using a ref parameter. 理解你的代码比使用ref参数更好。

Another benefit of return values is the ability to use method chaining. 返回值的另一个好处是使用方法链的能力。

You can write code like this which passes the list parameter from one method to another: 您可以编写这样的代码,将list参数从一个方法传递到另一个方法:

ListMethod1(list).ListMethod2(list)...

If you're just returning a List, you should always use the first one as it shows that intention. 如果你刚刚返回一个List,你应该总是使用第一个,因为它显示了这个意图。

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. 带有ref的版本告诉我,我可以从列表开始,你的方法将修改我发送的列表,甚至用另一个列表更改它。 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. 但是如果方法总是返回一个新列表,则使用返回值而不是ref参数。

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. 旁边的注释 :您可以使用out而不是ref来显示您返回新列表的意图,但如果您使用其他值的返回值,这只是一个很好的做法。

One thing special about ref Parameter here is that - "A variable passed with ref must be assigned value first.". 关于ref参数的一个特别之处在于 - “用ref传递的变量必须先赋值。”。 So if you want to make it strict for the calling method to assign value before the call you can probably use ref Parameters. 因此,如果您希望在调用之前对调用方法进行严格的赋值,则可以使用ref参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM