Shortcut for creating character array

26,291

Solution 1

Especially for multiple elements, the following shortcut is nice:

";".ToCharArray()

You can use this with multiple chars:

";,\t".ToCharArray()

Solution 2

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(',')
Share:
26,291
Daniel Mošmondor
Author by

Daniel Mošmondor

http://my_blog.net http://www.formsnapper.net http://www.spotcontrol.com http://www.videophill.com http://sourceforge.net/projects/mpg123net/ I work hard for every 5 points here :) and will shamelessly try to forward traffic to my projects. [email protected]

Updated on July 23, 2020

Comments

  • Daniel Mošmondor
    Daniel Mošmondor almost 4 years

    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...

  • Matthieu Charbonnier
    Matthieu Charbonnier over 6 years
    Great shortcut. I will never forget this one :)