[.net] [C#]Why can't you assign a char[] to a string

Started by
10 comments, last by turnpast 19 years, 5 months ago
Quote:Original post by Arild Fines
An array of char is a reference type. All arrays are reference types.


Good catch, and one that derails the remainder of my post.

Quote:Original post by Arild Fines
Specially treated how? String interning? The fact that you can have string literals?


Yes, the fact that a string variable is considered to be a primitive type. It's the only reference type that is also a primitive type, which combined with the primitive type ability to be assigned to literals, leads to interning. (For those who don't know interning is the process of maintaining an intern pool, which is a collection of each unique string literal in a program. This interning is why strings are immutable.)

I can't find the link at the moment but the MSDN blogs had an extensive posting about the changes made to the framework to support the above behavior. Thus, "specially treated."

Quote:Original post by Arild Fines
Since the char array is a reference type, there would be no boxing.


Yep, see first point. I've been knocked down to "I don't know!" status. :)

EDIT:
Here's Microsoft's "rule" on the matter:

Quote:
By eliminating unnecessary casts, implicit conversions can improve source code readability. However, because implicit conversions can occur without the programmer's specifying them, care must be taken to prevent unpleasant surprises. In general, implicit conversion operators should never throw exceptions and never lose information so that they can be used safely without the programmer's awareness. If a conversion operator cannot meet those criteria, it should be marked explicit.


I don't deal much with internationalization, but could a loss of data result if an implicit cast was allowed between a char and string type? The string type would depend upon the current culture, which the compiler wouldn't know about at the time of compilation (as it can be set by the end user). So you could be trying to stuff a Unicode char into a non Unicode string -- resulting in data loss. (The type of string or character created could be dependent upon a runtime setting as well.)

That's just a guess. Feel free to destroy it. :)
..what we do will echo throughout eternity..
Advertisement
Had MS decided to include an implicit conversion it might look like this:

public class String {   //blah blah blah ...  public static implicit operator string(char[] characters){    return new String(characters);  }}//and then you could:char[] chars = new char[]{'a','b','c'};string s = chars;//as opposed to:string s = new String(chars);//it is kind of like inducing confusion and not saving even one line of code.


In C# land an implicit cast does not always mean that we are using the same memory or the same internal structure. The implicit conversion above is confusing because you end up with two differnet references that really have no ongoing relationship like the relationship generally implied by reference assignment.

This topic is closed to new replies.

Advertisement