通过引用传递值是否可以显着提高速度? [英] Does passing values by reference improve speed significantly?

查看:103
本文介绍了通过引用传递值是否可以显着提高速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项:
按值传递与按引用传递性能C#.net

Possible Duplicates:
Pass by value vs Pass by reference performance C#.net

是否有人已经测试过通过引用传递参数是否比仅复制参数要快得多?

Did anyone already test if passing parameters by reference is significantly faster than just copying them?

但是问题的主要焦点是:使用ref关键字与不使用ref相对有什么缺点吗?

But the main focus of the question is: Are there any disadvantages using the ref keyword as opposite to not using it?

推荐答案

否,它不会显着提高速度,或者根本没有提高速度.相反,通过使用ref关键字,您将添加另一种间接访问级别,这只会使代码变慢.

No, it doesn't improve speed significantly, or anything at all. On the contrary, by using the ref keyword you are adding another level of indirection that only can make the code slower.

参数通常按值传递,这意味着它们已被复制.对于int这样的简单值,仅表示将值的副本放置在堆栈中.

Parameters are normally passed by value, which means that they are copied. For simple values like int, it simply means that a copy of the value is placed on the stack.

对于像string这样的引用类型,这意味着将引用的副本放置在堆栈中.因此,这并不意味着要复制整个对象,而只是对复制对象的引用.

For reference types like a string it means that a copy of the reference is placed on the stack. So, it doesn't mean that the entire object is copied, it's just the reference to the object that is copied.

除非有特殊原因,否则通常不应使用refout关键字.

You should generally not use the ref or out keywords, unless there is a special reason to do so.

这篇关于通过引用传递值是否可以显着提高速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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