仅初始化 c# 数组的几个值?
Initialization of only few values of c# array?
我正在尝试编写一个程序,我想在其中初始化一个数组的 4 个起始值,而其他 16 个将从控制台手动输入。
为此我编写了以下代码:
int[] intArray = new int[20] { 20, 22, 23, 0 };
但我收到以下异常:"An array initializer of '20' is expected"
我知道可以用这个语法来完成:
int[] intArray = new int[20];
intArray[0] = 20;
intArray[1] = 22;
intArray[2] = 23;
intArray[3] = 0;
我想知道这是否可以在一行中实现。
如果您经常需要这样做,您可以创建自己的扩展方法来填充现有数组中的值:
int[] intArray = new int[20].Populate(20, 22, 23, 0);
实施示例:
public static class ListExtensions
{
public static TList Populate<TList, TElement>(this TList list, params TElement[] values)
where TList : IList<TElement>
{
// TODO: argument validation
for (int i = 0; i < values.Length; i++)
list[i] = values[i];
return list;
}
}
这在两行中起作用:
int[] intArray = new int[20];
new int[4] { 20, 22, 23, 0 }.CopyTo(intArray , 0);
甚至:
int[] intArray = new int[20];
new int[] { 20, 22, 23, 0 }.CopyTo(intArray , 0);
请注意,未命名数组在分号后立即超出范围。-我认为它不会变得更简单!
我认为你可以这样做:
private static int[] getInput()
{
int[] array = { 20, 22, 23, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
bool done = false;
int current = 4;
while (!done)
{
Console.WriteLine("Enter in a number to be added to the array");
string input = Console.ReadLine();
array[current] = Convert.ToInt32(input);
current++;
if (current==array.Length)
{
done = true;
}
}
return array;
}
如何保持简单:
public static T[] CreateAndInitArray<T>( int len, T[] initialValues )
{
var temp = new T[len];
initialValues.CopyTo( temp, 0 );
return temp;
}
使用:
int[] intArray = CreateAndInitArray( 20, new int[] { 20, 22, 23, 0 } );
initialValues
参数也可以是 params
参数,但考虑到长度参数,这很容易混淆。我个人更喜欢道格拉斯提供的答案,看起来更合乎逻辑。
我发现 jon skeet 的建议更有用。
new[] { 20, 22, 23, 0 }.Concat(Enumerable.Repeat(0, 16)).ToArray();
我正在尝试编写一个程序,我想在其中初始化一个数组的 4 个起始值,而其他 16 个将从控制台手动输入。
为此我编写了以下代码:
int[] intArray = new int[20] { 20, 22, 23, 0 };
但我收到以下异常:"An array initializer of '20' is expected"
我知道可以用这个语法来完成:
int[] intArray = new int[20];
intArray[0] = 20;
intArray[1] = 22;
intArray[2] = 23;
intArray[3] = 0;
我想知道这是否可以在一行中实现。
如果您经常需要这样做,您可以创建自己的扩展方法来填充现有数组中的值:
int[] intArray = new int[20].Populate(20, 22, 23, 0);
实施示例:
public static class ListExtensions
{
public static TList Populate<TList, TElement>(this TList list, params TElement[] values)
where TList : IList<TElement>
{
// TODO: argument validation
for (int i = 0; i < values.Length; i++)
list[i] = values[i];
return list;
}
}
这在两行中起作用:
int[] intArray = new int[20];
new int[4] { 20, 22, 23, 0 }.CopyTo(intArray , 0);
甚至:
int[] intArray = new int[20];
new int[] { 20, 22, 23, 0 }.CopyTo(intArray , 0);
请注意,未命名数组在分号后立即超出范围。-我认为它不会变得更简单!
我认为你可以这样做:
private static int[] getInput()
{
int[] array = { 20, 22, 23, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
bool done = false;
int current = 4;
while (!done)
{
Console.WriteLine("Enter in a number to be added to the array");
string input = Console.ReadLine();
array[current] = Convert.ToInt32(input);
current++;
if (current==array.Length)
{
done = true;
}
}
return array;
}
如何保持简单:
public static T[] CreateAndInitArray<T>( int len, T[] initialValues )
{
var temp = new T[len];
initialValues.CopyTo( temp, 0 );
return temp;
}
使用:
int[] intArray = CreateAndInitArray( 20, new int[] { 20, 22, 23, 0 } );
initialValues
参数也可以是 params
参数,但考虑到长度参数,这很容易混淆。我个人更喜欢道格拉斯提供的答案,看起来更合乎逻辑。
我发现 jon skeet 的建议更有用。
new[] { 20, 22, 23, 0 }.Concat(Enumerable.Repeat(0, 16)).ToArray();