本文翻译自:How do I initialize an empty array in C#?
Is it possible to create an empty array without specifying the size? 是否可以在不指定大小的情况下创建一个空数组?
For example, I created: 例如,我创建了:
String[] a = new String[5];
Can we create the above string array without the size? 我们可以创建没有大小的上述字符串数组吗?
参考:https://stackoom.com/question/acKQ/如何在C-中初始化一个空数组
There is not much point in declaring an array without size. 声明没有大小的数组没有多大意义。 An array is about size . 一个数组大约是size 。 When you declare an array of specific size, you specify the fixed number of slots available in a collection that can hold things, and accordingly memory is allocated. 声明特定大小的数组时,请在集合中指定可容纳事物的固定可用插槽数,并相应地分配内存。 To add something to it, you will need to anyway reinitialize the existing array (even if you're resizing the array, see this thread ). 要向其中添加一些内容,无论如何您都需要重新初始化现有的数组(即使您要调整数组的大小,也请参见此线程 )。 One of the rare cases where you would want to initialise an empty array would be to pass array as an argument. 您想初始化一个空数组的罕见情况之一是将数组作为参数传递。
If you want to define a collection when you do not know what size it could be of possibly, array is not your choice, but something like a List
or similar. 如果要在不知道集合的大小的情况下定义集合,则数组不是您的选择,而是类似List
或类似的东西。
That said, the only way to declare an array without specifying size is to have an empty array of size 0 . 也就是说,声明一个不指定大小的数组的唯一方法是拥有一个大小为0的空数组。 hemant and Alex Dn provides two ways. hemant和Alex Dn提供了两种方法。 Another simpler alternative is to just : 另一个更简单的选择是 :
string[] a = { };
[ The elements inside the bracket should be implicitly convertible to type defined, for instance, string[] a = { "a", "b" };
[ 括号内的元素应隐式转换为定义的类型,例如string[] a = { "a", "b" };
] ]
Or yet another: 还是另一个:
var a = Enumerable.Empty().ToArray();
Here is a more declarative way : 这是一种更具声明性的方式 :
public static class Array
{
public static T[] Empty()
{
return Empty(0);
}
public static T[] Empty(int size)
{
return new T[size];
}
}
Now you can call: 现在您可以致电:
var a = Array.Empty();
//or
var a = Array.Empty(5);
You can do: 你可以做:
string[] a = { String.Empty };
Note: OP meant not having to specify a size, not make an array sizeless 注:OP意味着不必指定大小,而不是让一个数组没有尺寸
In .Net 4.6 the preferred way is to use a new method, Array.Empty
: 在.Net 4.6中,首选方法是使用新方法Array.Empty
:
String[] a = Array.Empty();
The implementation is succinct, using how static members in generic classes behave in .Net : 使用通用类中的静态成员在.Net中的行为, 实现是简洁的:
public static T[] Empty()
{
return EmptyArray.Value;
}
// Useful in number of places that return an empty byte array to avoid
// unnecessary memory allocation.
internal static class EmptyArray
{
public static readonly T[] Value = new T[0];
}
(code contract related code removed for clarity) (为清楚起见,删除了与代码合同相关的代码)
See also: 也可以看看:
Array.Empty
source code on Reference Source Array.Empty
源上的Array.Empty
源代码 Array.Empty()
Array.Empty()
Combining @nawfal & @Kobi suggestions: 结合@nawfal和@Kobi建议:
namespace Extensions
{
/// Useful in number of places that return an empty byte array to avoid unnecessary memory allocation.
public static class Array
{
public static readonly T[] Empty = new T[0];
}
}
Usage example: 用法示例:
Array.Empty
UPDATE 2019-05-14 更新2019-05-14
(credits to @Jaider ty) (归功于@Jaider ty)
Better use .Net API: 更好地使用.Net API:
public static T[] Empty ();
https://docs.microsoft.com/en-us/dotnet/api/system.array.empty?view=netframework-4.8 https://docs.microsoft.com/zh-cn/dotnet/api/system.array.empty?view=netframework-4.8
Applies to: 适用于:
.NET Core: 3.0 Preview 5 2.2 2.1 2.0 1.1 1.0 .NET Core:3.0预览版5 2.2 2.1 2.0 1.1 1.0
.NET Framework: 4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 .NET Framework:4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6
.NET Standard: 2.1 Preview 2.0 1.6 1.5 1.4 1.3 .NET标准:2.1预览版2.0 1.6 1.5 1.4 1.3
... ...
HTH 高温超导
简洁大方!
string[] array = {}