C# - 我应该使用"参考"通过参考的方法来传递的集合(例如,列表)? [英] c# - should I use "ref" to pass a collection (e.g. List) by reference to a method?

查看:132
本文介绍了C# - 我应该使用"参考"通过参考的方法来传递的集合(例如,列表)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用参考按引用传递一个列表变量的方法?

Should I use "ref" to pass a list variable by reference to a method?

是对答案在于不需要REF(作为列表将是一个参考变量),但是为了便于在可读性把REF,在?

Is the answer that "ref" is not needed (as the list would be a reference variable), however for ease in readability put the "ref" in?

推荐答案

没有,除非你想改变什么列出变量引用不使用参考。如果你只想访问列表中,这样做没有REF。

No, don't use a ref unless you want to change what list the variable is referencing. If you want to just access the list, do it without ref.

如果你犯了一个参数裁判,你是说方应期望他们通过在这个参数可以被分配到另一个对象。如果你不这样做,那么它没有传达正确的信息。你应该假设所有的C#开发人员了解对象引用正在被通过。

If you make a parameter ref, you are saying that the caller should expect that the parameter they pass in could be assigned to another object. If you aren't doing that, then it's not conveying the correct information. You should assume that all C# developers understand that an object reference is being passed in.

这篇关于C# - 我应该使用"参考"通过参考的方法来传递的集合(例如,列表)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆