1

In C#; I understand that numbers are different to signs... As I understand it, a byte conversion to an int is possible because the int type can read all of the byte binary compilation. However, why can't type char be as easily converted to string in the same way? Example:

char c = 'a';
string asdf = c; <== why do I have to use a ToString-method here?
Kalle
  • 133
  • 1
  • 2
  • 10
  • The conversion depends on the depends on the encoding (ASCII, UTF8, UTF16, etc.) you want. – Marker May 20 '18 at 12:46
  • Possible duplicate of [string type .NET vs. char array](https://stackoverflow.com/questions/6650699/string-type-net-vs-char-array) – Alexander May 20 '18 at 12:47
  • Good luck with the question. 'Why is it as it is)' questions often get closed/downvoted for being 'primarily opinion-based'. But with some luck you get a nice inside-story from the c# team, maybe even an anecdote from Eric Lippert or Jon Skeet.. – TaW May 20 '18 at 12:51
  • 1
    @Alexander How does that duplicate answers this question? – Camilo Terevinto May 20 '18 at 12:58
  • @CamiloTerevinto, perhaps, the comparison is not proper. But the topic contains posts that explain differense between `string` and `char` data types. – Alexander May 20 '18 at 13:06
  • 1
    It is possible but it doesn't exist because char and string are fundamentally so different that it's felt more important to emphasize their differences. It would be too easy for developers to confuse the two... When I first started many years ago I thought they were the same and if not for the concrete separation of concerns may have been even more confused later. Also, strings are immutable so the = would have an overload that had completely different logic for = char and other types as well. – Michael Puckett II May 20 '18 at 13:07
  • 2
    @Marker No, both `char` and `string` are UTF16 in .NET . – xanatos May 20 '18 at 13:30

1 Answers1

0

Well, because of the same reason int cannot be converted to int[], these are different types, and C# is a strongly typed programming language.

In fact, char is a numeric type and rules of numeric types conversion also apply to char, so it can be converted to int or long, but cannot be implicitly converted to string or char[], for example.

Alex Sikilinda
  • 2,928
  • 18
  • 34
  • @CamiloTerevinto because it cannot, neither explicitly nor implicitly, who told you it can? – Alex Sikilinda May 20 '18 at 13:07
  • How about `string t = "Item" + 1;` Many things are possible and munch syntatic sugar has been offered.. – TaW May 20 '18 at 13:21
  • 1
    @TaW Note that `string t = 1`; is illegal. It is legal only to concatenate (+) a string with anything else. – xanatos May 20 '18 at 13:22
  • 1
    I agre with Alex here... There are no implicit conversions from (anything) to `string`. `string x = 1000` is illegal. The only synctactic sugar that is present in .NET is the `+` operator that requires that only an operand is a `string` and the other can be anything (e.g. `5 + "x" == "5x", "x" + 5 == "x5"`) – xanatos May 20 '18 at 13:29
  • Hm, didn't think about it before, but you(@AlexSikilinda) are correct that char is a numeric type. True that, now I understand it better. Because I read about an explicit conversion of a "number" to get a character: char c = (char)97; //returns an "a" Thanks mate! But I do want to hear more from people before I accept an answer. – Kalle May 20 '18 at 22:54