C# 创建字符数组的快捷方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11604303/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:29:39  来源:igfitidea点击:

Shortcut for creating character array

c#array-initialization

提问by Daniel Mo?mondor

Since I like to Split()strings, I usually use

因为我喜欢Split()strings,所以我通常使用

new char[] { ';' }

or something like that for a parameter for Split().

或类似的参数用于Split().

Is there any shortcut for creating a character array with one element at compile time? Not that I mind typing, but...

在编译时创建具有一个元素的字符数组是否有任何快捷方式?不是说我介意打字,但是...

采纳答案by usr

Especially for multiple elements, the following shortcut is nice:

特别是对于多个元素,下面的快捷方式很好:

";".ToCharArray()

You can use this with multiple chars:

您可以将其与多个字符一起使用:

";,\t".ToCharArray()

回答by SLaks

In C# 3, you can use an implicitly-typed array:

在 C# 3 中,您可以使用隐式类型的数组:

new[] { ';' }

If you're not passing a StringSplitOptions, you can simply take advantage of the paramsparameter:

如果您没有传递 a StringSplitOptions,则可以简单地利用该params参数:

.Split(',')