通过参考传递变量.或价值困境 [英] Passing variable by Ref. or Value Dilemma

查看:76
本文介绍了通过参考传递变量.或价值困境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些无效的静态方法.

I have some void methods which are static.

最好通过引用或按值传递变量,因为我要将大量文本传递给这些变量:

Is it better to pass variables by reference or by value, because I'm passing large amount of text into these variables :

public static void renderText(ref StringBuilder build)
{
  //Do your job.
}

所以有人可以向我解释一下,当我发送StringBuilder的引用时会发生什么,它仅访问此StringBuilder吗? (它不会正确复制!).

So could someone explain me, what happens when I send a reference of StringBuilder, does it only access this StringBuilder? (It does not copy it right!).

以防万一我没有将输入参数的值或任何其他属性更改为方法.

And just in case I'm not changing the value or any other property of input arguments into methods.

因此,在这种情况下,变量足够大并且没有被操纵,我是否应该始终发送它的引用?如果是,它是否会干扰某些事物?

So, in this cases where variables are huge enough and not manipulated, should I always send the reference of it, and if yes does it interfere with something?

推荐答案

看看以下Jon Skeet的文章,其中他彻底解释了按引用传递或按值传递之间的区别.

Take a look at the following article by Jon Skeet in which he thoroughly explains the difference between passing by reference or by value.

在C#中传递参数

或说明前一篇文章的这篇博客文章:

or this blog post which illustrates the former article:

http://rapidapplicationdevelopment.blogspot.com/2007 /01/parameter-passing-in-c.html

上一篇文章实际上在其示例中使用了StringBuilder类型,因此您可以清楚地看到您的情况.如果通过引用传递StringBuilder类型,则会得到以下信息:

The last article actually uses the StringBuilder type in its examples, so you can clearly see what's going on in your case. If you pass in the StringBuilder type by reference you'll get this:

通过引用传递的引用类型:

Reference type passed by reference:

这篇关于通过参考传递变量.或价值困境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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