Can we please add support for String overloads wherever we see char[] in the framework?

Aaahhh!

One of my pet peeves, actually, is that sprinkled throughout the framework are many places that insist on taking a char[].  The instance method String.Split() is one such example, though I am certain that you will see many others. 

Why, oh why didn't we overload these methods to take a String?  It's not like I keep a char[] in my pocket, just waiting to use.  I've got to dust off a Char.Parse(), or some such ugly construct.

The String is one of the most natural objects in the framework.  Microsoft really got something right here, with a unified string type, instead of the mess of BSTR and LPSTR and char* and CString and wchar and TCHAR and LPCTSTR and basic_string and (breath, Jerry, breath!!)...

And, I'm not even mentioning the custom strings that people write for C++.  I think I would run out of blog space here.

So, since the String class is one of the most "natural" types in .NET, a string parameter should be the type of choice when passing charactor data.   Does anyone really have a good reason to offer a char[] param, and not a corresponding String overload?

4 Comments

  • ehh... I understand its a nuisance, but its not like its hard to fix.



    Mystring.Split("'".ToCharArray());

  • By definition, a string is zero, one OR MORE characters, in many cases, a function that requires only a single character makes sense.



    It is not hard to make a string a char using "a"c



    where the c suffix states that the aforementioend string literail is in fact a single character.

  • Jim, I wasn't speaking of a char, I was speaking of char[], which would potentially indicate the presence of more than one charactor.



    If the string was empty or null, then the behavior would be the same as if the char[] was null...



    I understand that it is easy to overcome, but the fact that I have to litter my code with unneccessary conversions is annoying.

  • Couple of notes. I wrote a pretty nice string splitting function if you follow the link posted above. Very fast, customizable for your own situation, and very memory conservative.



    With that said, there is a String.Split(string[]...) in Whidbey. It splits based on any number of sub-strings. It does not appear to be written quite as well as it could, but given that it is in the BCL, I imagine it will get a lot more usage than anything I might write and toss up on my blog.



Comments have been disabled for this content.