Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 引用类型常量初始化和数组浓缩_C#_.net_Constants - Fatal编程技术网

C# 引用类型常量初始化和数组浓缩

C# 引用类型常量初始化和数组浓缩,c#,.net,constants,C#,.net,Constants,1) const int[]数组={1,2,3,4}//这将产生以下错误 "Error 1 'ConsoleApplication1.Main.array' is of type 'int[]'. A const field of a reference type other than string can only be initialized with null" 根据errormessagge,我认为将const用于引用类型没有意义。我错了吗 2) 如何连接int数组?例如: int[

1)
const int[]数组={1,2,3,4}//这将产生以下错误

"Error 1 'ConsoleApplication1.Main.array' is of type 'int[]'.
 A const field of a reference type other than string can only be initialized with null"
根据errormessagge,我认为将const用于引用类型没有意义。我错了吗

2) 如何连接int数组?例如:

int[] x={1,2,3} + {4,5,6};

我知道+运算符不起作用,所以作为字符串使用它的最佳方法是什么?

Concat扩展方法。不是高清晰的代码,但确实如此

(new int[] { 1, 2, 3, 4 }).Concat(new int[] { 5, 6, 7, 8 }).ToArray();

Concat扩展方法可以做到这一点。不是高清晰的代码,但确实如此

(new int[] { 1, 2, 3, 4 }).Concat(new int[] { 5, 6, 7, 8 }).ToArray();
1) 是的,作为常量唯一有用的引用类型是字符串

2) 要连接数组,请创建一个新数组并将数组的内容复制到其中:

int[] a = { 1, 2, 3 };
int[] b = { 4, 5, 6 };

int[] x = new int[a.Length + b.Length];
a.CopyTo(x, 0);
b.CopyTo(x, a.Length);
我不知道你认为什么是“最好”的方法,但这是最有效的。(快速测试表明,这比使用
Concat
扩展方法快10-20倍。)

1)是的,作为常量唯一有用的引用类型是字符串

2) 要连接数组,请创建一个新数组并将数组的内容复制到其中:

int[] a = { 1, 2, 3 };
int[] b = { 4, 5, 6 };

int[] x = new int[a.Length + b.Length];
a.CopyTo(x, 0);
b.CopyTo(x, a.Length);

我不知道你认为什么是“最好”的方法,但这是最有效的。(快速测试表明,这比使用
Concat
扩展方法快10-20倍。)

我没有说性能点。我只是对可读性和清晰度感兴趣。我没有说性能的问题。我只是对可读性和清晰度感兴趣。