我知道int是一个值类型,但什么是值类型的数组?参考类型?价值类型?我想将一个数组传递给一个函数来检查一些东西.我应该只是传递数组,因为它只是传递它的引用,或者我应该将它传递给ref?
Yan*_*ton 193
数组是允许您将多个项目视为单个集合的机制.Microsoft®.NET公共语言运行库(CLR)支持单维数组,多维数组和锯齿状数组(数组数组).所有数组类型都是从System.Array隐式派生的,System.Array本身派生自System.Object.这意味着 所有数组始终 是在托管堆上分配的引用类型,并且应用程序的变量包含对数组的引用,而不是数组本身.
https://msdn.microsoft.com/en-us/library/bb985948.aspx
Ale*_*ort 29
参考类型与值类型的最简单测试是引用类型可以是null
,但值类型不能.
Jos*_*ins 14
数组(甚至像int这样的值类型)是C#中的引用类型.
http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx:
在C#中,数组实际上是对象.System.Array是所有数组类型的抽象基类型.
测试以验证它是引用类型还是值类型:
// we create a simple array of int
var a1 = new int[]{1,2,3};
// copy the array a1 to a2
var a2 = a1;
// modify the first element of a1
a1[0]=2;
// output the first element of a1 and a2
Console.WriteLine("a1:"+a1[0]); // 2
Console.WriteLine("a2:"+a2[0]); // 2
//**************************
// all the two variable point to the same array
// it's reference type!
//**************************
Run Code Online (Sandbox Code Playgroud)
您可以在线测试: https: //dotnetfiddle.net/UWFP45
小智 6
首先,我想告诉您Array是一种引用类型.为什么?我在这里解释一个例子.
例:
int val = 0; // this is a value type ok
int[] val1 = new int[20] // this is a reference type because space required to store 20 integer value that make array allocated on the heap.
Run Code Online (Sandbox Code Playgroud)
引用类型也可以为null,而值类型则不能.
您可以使用out或ref将数组传递给函数.只有初始化方法不同.
我想补充一下其他答案,虽然 int[] 是引用类型,但随着stackalloc
C# 中的引入,您可以将堆栈中的数组分配为值类型。这可能会给你带来性能提升,因为将数组放入堆栈可以减少 GC 压力(顺便说一句,一般来说,谈论值类型时,你可能经常听说值类型是在堆栈中分配的;但事实并非总是如此:
https: //learn.microsoft .com/en-us/archive/blogs/ericlippert/the-truth-about-value-types):
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/stackalloc
stackalloc 表达式在堆栈上分配一块内存。在方法执行期间创建的堆栈分配内存块将在该方法返回时自动丢弃。您无法显式释放使用 stackalloc 分配的内存。堆栈分配的内存块不受垃圾回收的影响,并且不必使用固定语句固定。
使用示例stackalloc
:
Span<int> numbers = stackalloc int[10];
for (int ctr = 0; ctr < numbers.Length; ctr++)
numbers[ctr] = ctr + 1;
foreach (int i in numbers)
Console.WriteLine(i);
Run Code Online (Sandbox Code Playgroud)
使用此技术时不要忘记有限的堆栈内存。链接https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/stackalloc提供了有关如何stackalloc
考虑此限制安全使用的必要信息。
此外,这里有一个讨论实际用法的答案stackalloc
:Practical use of `stackalloc` keywords