按值传递VS通过引用传递性能C#.NET [英] Pass by value vs Pass by reference performance C#.net
问题描述
我基本上创建一个轻量级的类,它是在10左右参数传递,它不会改变这些paramteres,它只是将它们存储在本地的构造函数时。
I'm basically create a lightweight class, it is passed in around 10 parameters, it does not change these paramteres, it just stores them locally when in the constructor.
有些是引用类型(字符串类)其他都是值类型(int,布尔,枚举)。
Some are reference types (strings, classes) others are value types (int, bool, enums).
我的问题是我应该通过这些所有的(除了我的课)使用关键字裁判?
My question is should I pass these all in (except my classes) with the keyword 'ref'?
在这里,我要考虑的是性能。
My consideration here is performance.
推荐答案
仅使用 REF
,如果该方法需要改变的参数,需要传递到这些变化调用code。如果您已经通过分析器运行它,并确定瓶颈的确是CLR方法的参数复制到堆栈中,你应该只优化这个。
Only use ref
if the method needs to alter the parameters, and these changes need to be passed onto the calling code. You should only optimize this if you have run it through a profiler and determined that the bottleneck is indeed the CLR copying the method parameters onto the stack.
记住的CLR在很大程度上调用带参数的方法进行了优化,所以我不应该认为这将是问题。
Bear in mind the CLR is heavily optimized for calling methods with parameters, so I shouldn't think this would be the issue.
这篇关于按值传递VS通过引用传递性能C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!