最佳实践:为ByRef或BYVAL?在.net [英] Best Practice: ByRef or ByVal? in .Net

查看:169
本文介绍了最佳实践:为ByRef或BYVAL?在.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么事情的ByRef和BYVAL之间进行选择时需要考虑的。

What are the things to consider when choosing between ByRef and ByVal.

我明白两者之间的差异,但我不完全明白,如果为ByRef节约了资源,或者我们甚至需要担心,在.NET环境。

I understand the difference between the two but I don't fully understand if ByRef saves resources or if we even need to worry about that in the .Net environment.

你如何决定两者之间,如果功能的情况不要紧?

How do you decide between the two if the functionality doesn't matter in a situation?

推荐答案

有很多误传围绕这一点。最主要的是,你理解值类型和引用类型和的差= http://pobox.com/~skeet/csharp/parameters.html">difference按值传递和按引用传递之间。

There's a lot of misinformation around about this. The main thing is that you understand the difference between value types and reference types, and the difference between pass by value and pass by reference.

您几乎总是要通过值。按引用传递几乎总是我想返回多个结果,而不是仅仅通过增加的事情是通过在列表中。通过使用按引用的方法的典型例子是 Int32.TryParse 那里的返回值是一个成功/失败,并分析得到的值是通过输出参数返回。

You almost always want to pass by value. Passing by reference is almost always for "I want to return more than one result, and not just by adding things to a list which is passed in." The classic example of a method using pass-by-reference is Int32.TryParse where the return value is a success/failure, and the parsed value is "returned" by an out parameter.

这篇关于最佳实践:为ByRef或BYVAL?在.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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