本文介绍了ToCharArray 和 ToArray 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ToCharArrayToArray

string mystring = "abcdef";

char[] items1 = mystring.ToCharArray();
char[] items2 = mystring.ToArray();

结果似乎是一样的.

推荐答案

string.ToCharArray() 是字符串类的成员.

string.ToCharArray() is a member of the string class.

string.ToArray() 实际上是在使用 a ToArray() IEnumerable 的扩展,利用了 string 实现了 IEnumerable.

string.ToArray() is actually using a ToArray() extension of IEnumerable<T>, taking advantage of the fact that string implements IEnumerable<char>.

在这两者中,string.ToCharArray() 的性能可能更高.

Of the two, string.ToCharArray() is likely to be more performant.

从C#参考源来看,string.ToCharArray()的实现是:

From the C# reference source, the implementation of string.ToCharArray() is:

unsafe public char[] ToCharArray() {
    // <
    int length = Length;
    char[] chars = new char[length];
    if (length > 0)
    {
        fixed (char* src = &this.m_firstChar)
            fixed (char* dest = chars) {
                wstrcpy(dest, src, length);
            }
    }
    return chars;
}

同样来自C#参考源,IEnumerable.ToArray()的实现是:

Also from the C# reference source, the implementation of IEnumerable<T>.ToArray() is:

public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source) {
    if (source == null) throw Error.ArgumentNull("source");
    return new Buffer<TSource>(source).ToArray();
}

...

struct Buffer<TElement>
{
    internal TElement[] items;
    internal int count;

    internal Buffer(IEnumerable<TElement> source) {
        TElement[] items = null;
        int count = 0;
        ICollection<TElement> collection = source as ICollection<TElement>;
        if (collection != null) {
            count = collection.Count;
            if (count > 0) {
                items = new TElement[count];
                collection.CopyTo(items, 0);
            }
        }
        else {
            foreach (TElement item in source) {
                if (items == null) {
                    items = new TElement[4];
                }
                else if (items.Length == count) {
                    TElement[] newItems = new TElement[checked(count * 2)];
                    Array.Copy(items, 0, newItems, 0, count);
                    items = newItems;
                }
                items[count] = item;
                count++;
            }
        }
        this.items = items;
        this.count = count;
    }

    internal TElement[] ToArray() {
        if (count == 0) return new TElement[0];
        if (items.Length == count) return items;
        TElement[] result = new TElement[count];
        Array.Copy(items, 0, result, 0, count);
        return result;
    }
}        

如您所见,这要复杂得多!

As you can see, that's a LOT more complicated!

为什么IEnumerable.ToArray()不使用优化路径?

Why doesn't IEnumerable<T>.ToArray() use the optimised path?

还有一件事我们需要解释.

There's one other thing we need to explain.

如果您检查 Buffer 的实现,您将看到此优化:

If you inspect the implementation of Buffer<T> you'll see this optimisation:

ICollection<TElement> collection = source as ICollection<TElement>;
if (collection != null) {
    count = collection.Count;
    if (count > 0) {
        items = new TElement[count];
        collection.CopyTo(items, 0);
    }
}    

你可以合理地问为什么不走这条路?如果是,这将是对 string.ToArray() 的一个很好的优化.

You could reasonably ask why that path isn't taken? If it was, this would be a good optimisation for string.ToArray().

嗯,答案很简单:string 没有实现 ICollection,因此 source as ICollection 将返回 null,则不会进行优化.

Well, the answer is simply: A string doesn't implement ICollection<T> and therefore source as ICollection<TElement> will return null, and that optimisation will not be done.

更糟糕的是,通过Buffer的非优化路径将使用string枚举器,其实现如下:

Even worse, the non-optimised path through Buffer<T> will use the string enumerator, which is implemented as follows:

public sealed class CharEnumerator : IEnumerator, ICloneable, IEnumerator<char>, IDisposable 
{
    private String str;
    private int index;
    private char currentElement;

    internal CharEnumerator(String str) {
        Contract.Requires(str != null);
        this.str = str;
        this.index = -1;
    }

    public Object Clone() {
        return MemberwiseClone();
    }

    public bool MoveNext() {
        if (index < (str.Length-1)) {
            index++;
            currentElement = str[index];
            return true;
        }
        else
            index = str.Length;
        return false;

    }

    public void Dispose() {
        if (str != null)
            index = str.Length;
        str = null;
    }

    /// <internalonly/>
    Object IEnumerator.Current {
        get {
            if (index == -1)
                throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted));
            if (index >= str.Length)
                throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded));                        

            return currentElement;
        }
    }

    public char Current {
        get {
            if (index == -1)
                throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted));
            if (index >= str.Length)
                throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded));                                            
            return currentElement;
        }
    }

    public void Reset() {
        currentElement = (char)0;
        index = -1;
    }
}


ICollection<TElement> collection = source as ICollection<TElement>;
if (collection != null) {
    count = collection.Count;
    if (count > 0) {
        items = new TElement[count];
        collection.CopyTo(items, 0);
    }
}    

这引入了另一个层面的低效率.

This introduces a whole other level of inefficiency.

这个故事的寓意

永远不要使用 IEnumerable.ToArray() 而不是 string.ToCharArray()

Never use IEnumerable<char>.ToArray() instead of string.ToCharArray()!

这篇关于ToCharArray 和 ToArray 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 10:55