Подтвердить что ты не робот

Разница между ToCharArray и ToArray

В чем разница между ToCharArray и ToArray

string mystring = "abcdef";

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

Результат, похоже, тот же.

4b9b3361

Ответ 1

string.ToCharArray() является членом класса string.

string.ToArray() фактически использует расширение a ToArray() IEnumerable<T>, воспользовавшись тем, что string реализует IEnumerable<char>.

Из двух, string.ToCharArray(), скорее всего, будет более результативным.

Из исходного источника С# реализация string.ToCharArray():

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;
}

Также из исходного источника С# реализация IEnumerable<T>.ToArray():

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;
    }
}        

Как вы можете видеть, это намного сложнее!

Почему IEnumerable<T>.ToArray() не использует оптимизированный путь?

Вот еще одна вещь, которую нам нужно объяснить.

Если вы проверите реализацию Buffer<T>, вы увидите эту оптимизацию:

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().

Ну, ответ просто: A string не реализует ICollection<T>, и поэтому source as ICollection<TElement> возвращает null, и что оптимизация не будет выполнена.

Хуже того, не оптимизированный путь через Buffer<T> будет использовать перечислитель string, который реализуется следующим образом:

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);
    }
}    

Это вводит совершенно другой уровень неэффективности.

Мораль этой истории

Никогда не используйте IEnumerable<char>.ToArray() вместо string.ToCharArray()!

Ответ 2

Метод ToCharArray:

ToCharArray метод для извлечения символов в строке массиву символов. Затем он отображает исходную строку и элементы в массиве.

using System;

public class Example
{
   public static void Main()
   {
      String s = "AaBbCcDd";
      var chars = s.ToCharArray();
      Console.WriteLine("Original string: {0}", s);
      Console.WriteLine("Character array:");
      for (int ctr = 0; ctr < chars.Length; ctr++)
         Console.WriteLine("   {0}: {1}", ctr, chars[ctr]);
   }
}

Метод ToArray:

Метод ToArray класса List, который действует на диапазоны.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        string[] input = { "Brachiosaurus", 
                           "Amargasaurus", 
                           "Mamenchisaurus" };

        List<string> dinosaurs = new List<string>(input);

        Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nAddRange(dinosaurs)");
        dinosaurs.AddRange(dinosaurs);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nRemoveRange(2, 2)");
        dinosaurs.RemoveRange(2, 2);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        input = new string[] { "Tyrannosaurus", 
                               "Deinonychus", 
                               "Velociraptor"};

        Console.WriteLine("\nInsertRange(3, input)");
        dinosaurs.InsertRange(3, input);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\noutput = dinosaurs.GetRange(2, 3).ToArray()");
        string[] output = dinosaurs.GetRange(2, 3).ToArray();

        Console.WriteLine();
        foreach( string dinosaur in output )
        {
            Console.WriteLine(dinosaur);
        }
    }
}