J'ai remarqué un comportement étrange lors de l'utilisation d'une combinaison de ToList() et ToArray() en C# (en utilisant le famework .NET 4.5.2).C# - comportement étrange avec ToList() et ToArray()
Compte tenu du programme de test suivant:
int[] array = { 5, 1, -10, 667, 4 };
SorteerBib<int>.SelectionSort(array, Comparer<int>.Default);
Console.Out.WriteLine("Result - WRONG!");
for (int i = 0; i < array.Length; i++)
{
Console.Out.Write(array[i] + " ");
Console.Out.WriteLine();
}
Console.ReadKey();
La classe SorteerBib se compose de 2 méthodes:
class SorteerBib<T>
{
public static IList<T> SelectionSort(IList<T> list, IComparer<T> comparer)
{
for (int i = 0; i < list.Count - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < list.Count; j++)
{
if (comparer.Compare(list[j], list[minIndex]) < 0)
{
minIndex = j;
}
}
T temp = list[i];
list[i] = list[minIndex];
list[minIndex] = temp;
}
Console.Out.WriteLine("Result SelectionSort IList - OK");
for (int i = 0; i < list.Count; i++)
{
Console.Out.Write(list[i]+" ");
Console.Out.WriteLine();
}
return list;
}
public static void SelectionSort(T[] array, IComparer<T> comparer)
{
// DOES NOT WORK, WHY?
array = ((SelectionSort(array.ToList(), comparer)).ToArray());
Console.Out.WriteLine("Result SelectionSort Array - OK");
for (int i = 0; i < array.Length; i++)
{
Console.Out.Write(array[i] + " ");
Console.Out.WriteLine();
}
}
}
L'erreur se situe dans la première ligne du deuxième méthode:
array = ((SelectionSort(array.ToList(), comparer)).ToArray());
Dans la méthode void, le tableau est trié après cette ligne, mais une fois que nous revenons au prog principal ram le tri est parti.
Le remplacement de cette ligne avec les lignes fixe cependant cette question ci-dessous:
IList<T> temp = SelectionSort(array.ToList<T>(), comparer);
for (int i = 0; i < array.Length; i++) {
array[i] = temp[i];
}
Quelqu'un peut-il expliquer ce comportement imprévisible? Merci beaucoup d'avance!
Il n'y a pas de comportement imprévisible, passez le tableau comme byref, alors ça va marcher, la deuxième approche fonctionne parce que vous remplacez des éléments dans le tableau, pas le tableau lui-même. – Gusman
Copie possible de [ToList() - Crée-t-il une nouvelle liste?] (Http://stackoverflow.com/questions/2774099/tolist-does-it-create-a-new-list) – Juan
Les tableaux FYI implémentent ' IList ', donc pas besoin de la seconde méthode ou d'appeler' ToList' lors du passage d'un tableau à la première. –
juharr