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

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

char[] c = /* some string */;
string s = c; // doesn't work
// have to do
string s2 = new string(c);


Is there any reason no conversion exists?

##### Share on other sites
There's no implicit conversion; that's all.
Most of the time I use a StringBuffer to add characters.

Cheers

##### Share on other sites
I know it didn't work because there was no implicit conversion, I wondered about the reason it didn't exist :)

##### Share on other sites
There probably isn't an implicit conversion because that would imply that the process is trivial (most implicit conversions are reletively non-intensive). But since strings are immutable and char arrays aren't, the data from the array must be copied into a newly allocated buffer for the string, which isn't nearly as trivial as converting an int to a long for example. Just my take on it, but it seems to make sense.

##### Share on other sites
The CLR performs additional optimizations with strings that are not possible with arrays.
One example is the internal string pool used to conserve memory by not keeping duplicates. Take a look at the Intern method of string for more information.
There are probaly alot of other things that it does too. It may for instance perform compression or have methods of search/compare optimization.

##### Share on other sites
There is no implicit conversion as such, but there is a constructor that takes char[]

##### Share on other sites
I used that constructor in the code in my first post.

##### Share on other sites
I'd be more surprised if there was such a conversion.

##### Share on other sites
char is a value type. string is a reference type, albeit a specially treated one. Converting between them involves boxing the char array as a string. I can't think of any conversion that requires boxing that is done without explicit casting.

##### Share on other sites
Quote:
 Original post by Taloniuschar is a value type.

An array of char is a reference type. All arrays are reference types.
Quote:
 string is a reference type, albeit a specially treated one.

Specially treated how? String interning? The fact that you can have string literals?
Quote:
 Converting between them involves boxing the char array as a string.

Since the char array is a reference type, there would be no boxing.

##### Share on other sites
Quote:
 Original post by Arild FinesAn 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 FinesSpecially 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 FinesSince 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. :)

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.