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

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

问题描述

我应该使用ref"通过引用方法来传递列表变量吗?

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# - 我应该使用“ref"吗?通过引用方法传递集合(例如列表)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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