Why is there no Char.Empty like String.Empty?

2019-01-04 00:37发布

Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings.

Edit: The reason for this usage was this:

myString.Replace ('c', '')

So remove all instances of 'c's from myString.

19条回答
小情绪 Triste *
2楼-- · 2019-01-04 01:15

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

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-04 01:16

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.

查看更多
4楼-- · 2019-01-04 01:16

Use Char.MinValue which works the same as '\0'. But be careful it is not the same as String.Empty.

查看更多
不美不萌又怎样
5楼-- · 2019-01-04 01:17

use

myString.Replace ("c", "")
查看更多
趁早两清
6楼-- · 2019-01-04 01:22

A char is a value type, so its value cannot be null. (Unless it is wrapped in a Nullable container).

Since it can't be null, in contains some numeric code and each code is mapped to some character.

查看更多
SAY GOODBYE
7楼-- · 2019-01-04 01:26

Easiest way to blanket remove a character from string is to Trim it

cl = cl.Trim(' ');

Removes all of the spaces in a string

查看更多
登录 后发表回答