默认情况下,在C#中引用传递数组或列表? [英] Are arrays or lists passed by default by reference in c#?

查看:122
本文介绍了默认情况下,在C#中引用传递数组或列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的吗?或加快我的程序,我应该通过引用传递呢?

Do they? Or to speed up my program should I pass them by reference?

推荐答案

参考传递的通过值

在.NET数组是堆对象,所以你有一个参考。这一基准是按值传递,这意味着到阵列的内容的变化将被调用者观察,但重新分配数组不会:

Arrays in .NET are object on the heap, so you have a reference. That reference is passed by value, meaning that changes to the contents of the array will be seen by the caller, but reassigning the array won't:

void Foo(int[] data) {
    data[0] = 1; // caller sees this
}
void Bar(int[] data) {
    data = new int[20]; // but not this
}

如果您添加 REF 改性剂,在参考传递的引用的 - 并且调用者会看到无论是改变以上。

If you add the ref modifier, the reference is passed by reference - and the caller would see either change above.

这篇关于默认情况下,在C#中引用传递数组或列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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