引用类型常量初始化和数组连接
1) const int[] array={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"
在我看来,根据错误信息,使用 const 作为引用类型是没有意义的。我错了吗?
2)如何连接int数组?示例:
int[] x={1,2,3} + {4,5,6};
我知道 + 运算符不起作用,那么将其作为字符串执行的最佳方法是什么?
1) const int[] array={1,2,3,4}; //this gives below error
"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"
In my opinion according to error messeagge, it is not meaningfull to use const for reference types.Am i wrong ?
2) How can i concatenate int array ? Example:
int[] x={1,2,3} + {4,5,6};
I know that + operator will not work so what is best way to do it as strings ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Concat 扩展方法可以做到这一点。代码清晰度不高,但确实如此。
Concat extension method do that. Not high clarity code but does.
1) 是的,唯一可用作常量的引用类型是 String。
2)要连接数组,您创建一个新数组并将数组的内容复制到其中:
我不知道您认为什么是“最佳”方法,但这是最有效的。 (快速测试表明,这比使用
Concat
扩展方法快 10-20 倍。)1) Yes, the only reference type that is any useful as constant is String.
2) To concatenate arrays you create a new array and copy the contents of the arrays to it:
I don't know what you count as the "best" method, but this is the most effective. (A quick test shows that this is 10-20 times faster than using the
Concat
extension method.)