• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

通过参考传递变量.或价值困境

用户头像
it1352
帮助1

问题说明

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

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?

正确答案

#1

看看以下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

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:

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfiecb
系列文章
更多 icon
同类精品
更多 icon
继续加载