10

I'm cleaning up some old code that's repeatedly doing myString.ToCharArray().Length; instead of myString.Length.

Before I refactor ToCharArray() out, are there any circumstances where doing so would result in different behavior?

4
  • I think it's hard to imagine every possible scenario. I think it's one of those things where you'll have to test and see. Commented Mar 22, 2013 at 15:22
  • @user1477388 I've already looked at trivial cases. If there was going to be an exception it would be in the real of 'interesting' unicode behavior; and not normal everyday cases. Iterating over the entire charset to brute force a few billion test strings would take much longer than asking the question. Commented Mar 22, 2013 at 15:29
  • @DanNeely See stackoverflow.com/questions/5495523/… Commented Mar 22, 2013 at 15:30
  • @voo since I'm not dumping to bytes anywhere that doesn't appear to be relevant. Commented Mar 22, 2013 at 15:37

4 Answers 4

9

No difference : have a look the method code with reflector : it will allocate a char[] based on the length of the string.

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

3 Comments

Specifically, both the original string and the char[] returned by ToCharArray() will be a UTF-16 representation of the string contents. The length in both cases is the number of UTF-16 code points required to represent the text, so it will match.
The second can have NullReferenceExceptions too
What does the NullReferenceException note have to do with anything? This would happen in both cases were the string null.
3

Both will definitely return the same length, representing the number of characters (chars). There'll be one char per each index of the string (or array), and, then, the following could be worthy of mention:

The Length property returns the number of Char objects in this instance, not the number of Unicode characters. The reason is that a Unicode character might be represented by more than one Char. Use the System.Globalization.StringInfo class to work with each Unicode character instead of each Char.

From this you can also deduce that ToCharArray, given that it is "A Unicode character array whose elements are the individual characters of this instance", that they behave the same.

I'm not sure what the not on NullReferenceException is about, as both are susceptible to that, too.

Comments

2

result in different behavior?

Well, as others said, in both ways, result will be the number of char, so, the answer is definitly NO!

But I want to add something different to this answer.

When I decompile String.ToCharArray() method, it looks like:

public unsafe char[] ToCharArray()
{
  int length = this.Length;
  char[] chArray = new char[length];
  if (length > 0)
  {
    fixed (char* smem = &this.m_firstChar)
      fixed (char* dmem = chArray)
        string.wstrcpyPtrAligned(dmem, smem, length);
  }
  return chArray;
}

ToCharArray uses unsafe code that manipulates pointers, along with the private wstrcpyPtrAligned method in the base class library. It is normally faster than doing the same thing in managed code, which has to check array bounds.

But

It makes a complete pass over the string, so if you do not require that, filling character arrays manually may be faster.

Since ToCharArray() method returns a char array, which you can modify in-place. This sometimes improves the performance of code.

1 Comment

I don't think the mutability of the thing is in question, and, as you can see, the ToCharArray method checks the length of the string anyway, so there's certainly not going to be a performance gain.
0

I don' think it will give different result in any scenario!

but if I use its overloaded method i.e. ToCharArray(int startIndex, int length) then obviously it can give different value of length property as you give the length.

Also I can not override this ToCharArray() method in my custom class like ToString() method. So If you want to calculate the length only, then using myString.Length makes more sense than using myString.ToCharArray().Length.

1 Comment

We can not override ToCharArray() method like ToString() method. I will overrride the ToString() method to add some extra logic in my class.

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.