1

Is there any specific reason why there is no empty char literal?

What comes closest to what I think of, the '' is the '\0' the null character.

In C++ the char is represented by an int, which means empty char goes directly to the 0 integer value, which is in C++ "the same as null".

The practical part of coming up with that question:

In a class I want to represent char values as enum attributes. Unbiased I tried to initialize an instance with '', which of course does not work. But shouldn't be there a char null value? Not to be confused with string.Empty, more in the nature of a null reference.

So the question is: Why is there no empty char?

-edit-

Seeing this question the question can be enhanced on: An empty char value would enable concatening strings and chars without destroying the string. Would that not be preferable? Or should this "just work as expected"?

Community
  • 1
  • 1
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • Possible duplicate: http://stackoverflow.com/questions/3670505/why-is-there-no-char-empty-like-string-empty – Jeroen Vannevel Jul 16 '13 at 19:08
  • No duplicate, as the question goes on the Char class, where I want to know about the design decision behind that. – Mare Infinitus Jul 16 '13 at 19:08
  • 2
    char is a primitive type. A null char makes no more sense than a null int – bengoesboom Jul 16 '13 at 19:12
  • I already saw that comment, so what about `int?` Does this not make sense either? This is clearly a design decision and there are certainly clear reasons for that. A null char would be... empty. The total sum of all character in string.Empty. – Mare Infinitus Jul 16 '13 at 19:17

2 Answers2

5

A char by definition has a length of one character. Empty simply doesn't fit the bill.

Don't run into confusion between a char and a string of max length 1. They sure look similar, but are very different beasts.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • 3
    This makes `char` sound like a `string` whose length is restricted to 1. It's not. The key point is that `char` is a value type. – dtb Jul 16 '13 at 19:11
  • No, I'm not confusing it, this is the reason for my question. Should there not be an empty char, just like there is a null reference? – Mare Infinitus Jul 16 '13 at 19:12
  • @dbt: The value type approach seems to make the most sense. But still I think a null value would make sense, but here the `?` comes in, which does not provide the semantics I would expect from a char null value. But keeping to what already is out there is often a very good idea, so it behaves mostly like C++ or Java and developers do not have to rethink that much. – Mare Infinitus Jul 16 '13 at 21:00
  • @dtb Well, I wrote *"They sure look similar, but are very different beasts."*, so making them sound alike was not my intention – Eugen Rieck Jul 16 '13 at 21:19
2

To give a slightly more technical explanation: There is no character that can serve as the identity element when performing concatenation. This is different from integers, where 0 serves as the identity element for addition.

Douglas
  • 53,759
  • 13
  • 140
  • 188
  • as 1 does for multiplication, and this is exactly what I'm asking. Why is it not there? – Mare Infinitus Jul 16 '13 at 19:13
  • 1
    @MareInfinitus concatenating two chars 'a' and 'b' would not give a char. Why would you expect there to be a special value where it would? – bengoesboom Jul 16 '13 at 19:15
  • @Lee: Read the last part of the question (after it was edited). This is all about identities. – Douglas Jul 16 '13 at 19:15
  • @bengiesboom: I would expect concatening `'a'` and `''` to give a string of length one. – Mare Infinitus Jul 16 '13 at 19:19
  • More concrete: I would expect concatening `'a'` + `''` + `'b'` to give a string of length two, whereas `'a'` + `'\0'` + `'b'` gives a string of length one. – Mare Infinitus Jul 16 '13 at 19:36
  • I don't think it is fair to compare charactor concatenation to integer addition. You can add the null charator to any charactor and receive the same charactor (requires a cast in C#). Also, there is no integer that serves as the identity element when performing _concatenation_. If you attempt to concatenate a zero, you will be adding the charactor `0` to the end (or begining). – Cemafor Jul 16 '13 at 19:39
  • Integer addition and char concatenation have not that much in common. Where do you think I am comparing those? – Mare Infinitus Jul 16 '13 at 20:18