classDoubleInt
{
publicint int1, int2;
}
varliste = newList<DoubleInt>() {
newDoubleInt() { int1 = 5, int2 = 1 },
newDoubleInt() { int1 = 2, int2 = 2 },
newDoubleInt() { int1 = 4, int2 = 3 } };
// Calling the Sort() method results in the use of the default comparer for the DoubleInt type// which is the IComparable.CompareTo method
liste.Sort();
// System.InvalidOperationException: No IComparable<T> or IComparable interface found for type 'DoubleInt'.
// delegate anonyme 1
liste.Sort( (di1, di2) => {
return di1.int1.CompareTo(di2.int1);
} );
// named function
liste.Sort(CompareDoubleInt);
staticintCompareDoubleInt(DoubleIntdi1, DoubleIntdi2)
{
if (di1 == null) // if item1 is null
{
if (di2 == null) return0; // and item2 is null, they're equal.elsereturn-1; // and item2 is not null, item2 is greater.
}
else// if item1 is not null
{
if (di2 == null) return1; // and item2 is null, item1 is greater.else// and item2 is not null :
{
return di1.int1.CompareTo(di2.int1);
}
}
}
List<B> lb; // avec B héritant de AList<A> la = lb; // error: pas de covariance avec List<T>IList<A> la = ilb; // error: pas de covariance avec IList<T>IEnumerable<B> ieb = lb.Select(b => b);
IEnumerable<A> iea = ieb; // ok: covariance avec IEnumerable<T>IReadOnlyCollection<B> lbr = lb.AsReadOnly();
IReadOnlyCollection<A> lar = lbr; // ok: covariance avec IReadOnlyCollection<T>
// prend en compte l'ordre l'ordre des élémentsboolareEquals = myList1.SequenceEqual(myList2);
// sans prendre en compte l'ordre des éléments// il faut tester count, car retourne true si myList2 contient plus d'éléments que myList1boolareEquals = myList1.Count == myList2.Count && myList1.All(myList2.Contains);
varareNotEquals = !myList1.All(myList2.Contains);
LinkedList est composé d'élément dont chacun pointe vers le précédent et le suivant (comme une chaîne). Il est donc peut coûteux d'ajouter ou de supprimer des élément. Mais l'accès à un élément nécessite le parcourt de la liste.
List est un wrapper autour d'un Array, l'ajout ou la suppression d'éléments peut nécessiter un redimensionnement du tableau interne. Mais l'accès à un élément se fait directement via son index.
ReadOnlyCollection
ReadOnlyCollection.cs
// in the .NET frameworkpublicclassReadOnlyCollection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>
{
// only a ctor with IList<T>publicReadOnlyCollection(IList<T> list) { /* ... */ }
}