22

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

new char[] { ';' }

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

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

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99

2 Answers2

39

Especially for multiple elements, the following shortcut is nice:

";".ToCharArray()

You can use this with multiple chars:

";,\t".ToCharArray()
usr
  • 168,620
  • 35
  • 240
  • 369
21

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

new[] { ';' }

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

.Split(',')
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964