Why is there no Char.Empty like String.Empty?
There's no such thing as an empty char. The closest you can get is '\0'
, the Unicode "null" character. Given that you can embed that within string literals or express it on its own very easily, why would you want a separate field for it? Equally, the "it's easy to confuse ""
and " "
" arguments don't apply for '\0'
.
If you could give an example of where you'd want to use it and why you think it would be better, that might help...
The reason for this usage was this:
myString.Replace ('c', '')
So remove all instances of'c'
from myString.
To remove a specific char from a string you can use the string overload:
myString = myString.Replace ("c", String.Empty);
Your statement
myString.Replace ('c', '\0')
Won't remove any characters. It will just replace them with '\0'
(End-Of-String, EOS), with varying consequences. 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.
A char, unlike a string, is a discrete thing with a fixed size. A string is really a container of chars.
So, Char.Empty doesn't really make sense in that context. If you have a char, it's not empty.
There's no such thing as an empty character. It always contains something. Even '\0' is a character.
Use Char.MinValue
which works the same as '\0'. But be careful it is not the same as String.Empty
.