C# int[] 是引用类型还是值类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1533757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is int[] a reference type or a value type?
提问by devoured elysium
I know an int is a value type, but what are arrays of value types? Reference types? Value types? I want to pass an array to a function to check something. Should I just pass the array, as it will just pass the reference of it, or should I pass it as ref?
我知道 int 是一种值类型,但什么是值类型数组?引用类型?值类型?我想将数组传递给函数以检查某些内容。我应该只传递数组,因为它只会传递它的引用,还是应该将它作为 ref 传递?
采纳答案by Yannick Motton
Arrays are mechanisms that allow you to treat several items as a single collection. The Microsoft? .NET Common Language Runtime (CLR) supports single-dimensional arrays, multidimensional arrays, and jagged arrays (arrays of arrays). All array types are implicitly derived from System.Array, which itself is derived from System.Object. This means that all arrays are always reference typeswhich are allocated on the managed heap, and your app's variable contains a reference to the array and not the array itself.
数组是允许您将多个项目视为单个集合的机制。微软?.NET 公共语言运行时 (CLR) 支持一维数组、多维数组和交错数组(数组的数组)。所有数组类型都是从 System.Array 隐式派生的,而 System.Array 本身又是从 System.Object 派生的。这意味着 所有数组始终是在托管堆上分配的引用类型,并且您的应用程序的变量包含对数组的引用而不是数组本身。
回答by Joshua Tompkins
Arrays (even of value types like int) are reference types in C#.
数组(甚至像 int 这样的值类型)是 C# 中的引用类型。
http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx:
http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx:
In C#, arrays are actually objects. System.Array is the abstract base type of all array types.
在 C# 中,数组实际上是对象。System.Array 是所有数组类型的抽象基类型。
回答by Alex Reitbort
The simplest test for reference type vs. value type is that reference types can be null
, but value types can not.
引用类型与值类型的最简单测试是引用类型可以是null
,但值类型不能。
回答by Scott Dorman
The array itself is a reference type. The values of that array are value or reference types as determined by the array data type. In your example, the array is a reference type and the values are value types.
数组本身是一种引用类型。该数组的值是由数组数据类型确定的值或引用类型。在您的示例中,数组是引用类型,值是值类型。
All single-dimension arrays implicitly implement IList<T>
, where <T>
is the data type of the array. You can use that interface as the data type of your method parameter instead. You could also use IEnumerable<T>
for the data type. In either case (or even if you just use int[]
) you shouldn't need to explicitly pass it as a ref
parameter.
所有单维数组都隐式实现IList<T>
,其中<T>
是数组的数据类型。您可以使用该接口作为方法参数的数据类型。您也可以IEnumerable<T>
用于数据类型。在任何一种情况下(或者即使您只是使用int[]
),您都不需要将其作为ref
参数显式传递。
回答by Domi
//The reference to the array is passed by value. This is the source of the confusion :-) ...
//对数组的引用是按值传递的。这是混乱的根源:-) ...
int[] test = { 1, 2, 3, 4 };
modifContenuSansRef(test);
Console.WriteLine(test[0]); // OK --> 99 le contenu du tableau est modifié
modifTailleSansRef(test);
Console.WriteLine(test.Length); // KO --> 4 La taille n'est pas modifiée
}
static void modifContenuSansRef(int[] t)
{
t[0] = 99;
}
static void modifTailleSansRef(int[] t)
{
Array.Resize(ref t, 8);
}
回答by Devendra Gohel
First I want to tell you that Array is a reference type. Why? I explain throw one example over here.
首先我想告诉你 Array 是一个引用类型。为什么?我在这里解释抛出一个例子。
Example:
例子:
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.
Also reference types can be null whereas value types can't.
引用类型也可以为空,而值类型不能。
value type stored in Stack and reference type stored in Heap
You can pass array to function using out or ref. Only initialize methods are different.
您可以使用 out 或 ref 将数组传递给函数。只有初始化方法不同。
回答by So_oP
Arrays always are reference types.it does not matter array would contain value type like int or reference type like string.When you declare array for example
数组总是引用类型。数组是否包含像 int 这样的值类型或像 string 这样的引用类型并不重要。例如,当你声明数组时
int[] integers=new int[10]
;
int[] integers=new int[10]
;
integers variable itself contains only reference to the array which will reside in heap.
整数变量本身只包含对将驻留在堆中的数组的引用。
Also there is many people mention that you could differ value type from reference type just depend on the fact thhat variable could be null or not. I would like to mention that in the c# currently value types also can be null
还有很多人提到你可以将值类型与引用类型不同,这取决于变量是否为空这一事实。我想提一下,在 c# 中当前值类型也可以为 null
for instance
例如
int? integer=null
and it is not good way to identify the type is reference or value only depends on the fact variable could be null or not.
并且这不是确定类型是引用还是值的好方法,这仅取决于变量是否为空的事实。
回答by Marco Staffoli
Test to verify if it's a reference or value type:
测试以验证它是引用类型还是值类型:
// 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!
//**************************
You can test it online: https://dotnetfiddle.net/UWFP45
你可以在线测试:https: //dotnetfiddle.net/UWFP45
回答by Rahul Bhat
Just a bit of an insight:
只是一点见解:
For example, int
represents a single integer, int[]
represents an array of integers.
例如,int
代表单个整数,int[]
代表一个整数数组。
To initialize the array with specific dimensions, you can use the new
keyword, giving the size in the square brackets after the type name:
要初始化具有特定维度的数组,您可以使用new
关键字,在类型名称后的方括号中给出大小:
//create a new array of 32 ints.
int[] integers = new int[32];
All arrays are reference types and follow reference semantics. Hence, in this code, even though the individual elements are primitive value types, the integers
array is a reference type. So if you later write:
所有数组都是引用类型并遵循引用语义。因此,在此代码中,即使单个元素是原始值类型,integers
数组也是引用类型。所以如果你以后写:
int[] copy = integers;
this will simply assign the whole variable copy to refer to the same array, it won't create a new array.
这将简单地将整个变量副本分配给同一个数组,它不会创建一个新数组。
C#'s array syntax is flexible, it allows you to declare arrays without initializing them so that the array can be dynamically sized later in the program. With this technique, you are basically creating a null reference and later pointing that reference at a dynamically allocated stretch of memory locations requested with a new
keyword:
C# 的数组语法很灵活,它允许您声明数组而无需初始化它们,以便稍后可以在程序中动态调整数组大小。使用这种技术,您基本上是在创建一个空引用,然后将该引用指向使用new
关键字请求的动态分配的内存位置:
int[] integers;
integers = new int[32];
Thank You.
谢谢你。