Coder Perfect

I’m not sure why there isn’t a Char. Like String, it’s empty. Empty?


Is there a rationale for this? I’m curious because if you needed to use a lot of empty chars, you’d be in the same boat as if you needed to use a lot of empty strings.

Edit: This was done for the following reason:

myString.Replace ('c', '')

As a result, remove all ‘c’s from myString.

Asked by Joan Venge

Solution #1

There isn’t such a thing as a blank char. ‘0,’ the Unicode “null” character, is the closest you can get. Why would you want a separate field for it when you can easily embed it within string literals or express it on its own? The “it’s easy to mix up “” and ” “” arguments also don’t apply to ‘0’.

It could help if you could offer an example of where you’d like to use it and why you think it’d be better…

Answered by Jon Skeet

Solution #2

The string overload can be used to remove a specific character from a string:

 myString = myString.Replace ("c", String.Empty);

Your statement

 myString.Replace ('c', '\0')

No characters will be removed. It will just substitute ‘0’ (End-Of-String, EOS) for them, with variable results. Some string operations might stop when encountering an EOS but in .NET most actions will treat it like any other char. Best to avoid ‘\0’ as much as possible.

Answered by Henk Holterman

Solution #3

Unlike a string, a char is a separate object with a definite size. A string is nothing more than a collection of characters.

So there you have it, Char. In that context, the word “empty” doesn’t make any sense. It’s not empty if you have a char.

Answered by Joe

Solution #4

There is no such thing as a character that is empty. It always has something in it. Even the number ‘0’ is a character.

Answered by Philippe Leybaert

Solution #5

Use Char.MinValue, which is equivalent to ‘0’. However, it is important to note that it is not the same as String. Empty.

Answered by Aliostad

Post is based on