14
        char character = 'c';
        string str = null;
        str = character.ToString();//this is ok

        char[] arrayChar = { 'a', 'b', 'c', 'd' };
        string str2 = null;
        str2 = string.Copy(arrayChar.ToString());//this is not ok
        str2 = arrayChar.ToString();//this is not ok.

I'm trying to converting char array to string, but the last two attempts don't work. Other source I found and they have to create new string type, but I don't know why. Can someone give me little explaination, thanks.

3 Answers 3

29

You need to construct a new string.

Doing arrayChar.ToString() calls the "ToString" method for the char[] type, which is not overloaded to construct a string out the characters, but rather to construct a string that specifies that the type is an array of characters. This will not give you the behavior that you desire.

Constructing a new string, via str2 = new string(arrayChar);, however, will give you the behavior you desire.

The issue is that, in C# (unlike C++), a string is not the same as an array of characters. These are two distinctly different types (even though they can represent that same data). Strings can be enumerated as characters (String implements IEnumerable<Char>), but is not, as far as the CLR is concerned, the same type as characters. Doing a conversion requires code to convert between the two - and the string constructor provides this mechanism.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, in C++, a "string" is not an "array of characters". Please be a little more precise. In C++ "string" is a class (std::string, std::wstring) which happens to have an implicit conversion construtor from a character array. This is far different than "being a char array". Or, if you've been talking about a c-string, then that's a C-language notion of strings encoded as null-terminated character array. C. Not C++. Different languages. Other than that, all's fine.
9
new string(arrayChar);

6 Comments

Isn't that what you want to do?
@Bopha: I tried to explain this in my answer for you - does that help? @Andrey: +1 for being the correct answer, too :)
yep that is what I want to do, but wonder why I have to create new string to set array char to string. Because I thought string is just a sequence of character representing like array. The reason of that because I can access individual character in string by applying the bracket and index position.
@Bopha - because char[] is mutable and string is not. this is the way strings are designed and implemented in .net
@Bopha: In .NET, strings are immutable, which means you can't "set" anything to an existing string. The only way is to create a new string.
|
1

There is a string constructor that takes a char array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.