Initialiseur
|
var listInt = new List<int> { 4, 5, 6, 7 };
|
Méthode Find
|
static void Main(string[] args)
{
var liste = new List<DoubleInt>() {
new DoubleInt() { int1 = 0, int2 = 1 },
new DoubleInt() { int1 = 1, int2 = 2 },
new DoubleInt() { int1 = 2, int2 = 3 } };
int two = 2;
var doubleInt = liste.Find( delegate(DoubleInt di) { return di.int1 == two; } );
var liste2 = liste.FindAll(ContainsOne);
}
static bool ContainsOne(DoubleInt di) { return (di.int1 == 1 || di.int2 == 1); }
class DoubleInt { public int int1, int2; }
|
|
class DoubleInt
{
public int int1, int2;
}
var liste = new List<DoubleInt>() {
new DoubleInt() { int1 = 5, int2 = 1 },
new DoubleInt() { int1 = 2, int2 = 2 },
new DoubleInt() { int1 = 4, int2 = 3 } };
liste.Sort();
|
IComparable
|
class DoubleInt : IComparable<DoubleInt>
{
public int int1, int2;
public int CompareTo(DoubleInt other)
{
return int1.CompareTo(other.int1);
}
}
|
delegate int Comparison<in T>(T x, T y)
|
liste.Sort( (di1, di2) => {
return di1.int1.CompareTo(di2.int1);
} );
liste.Sort(CompareDoubleInt);
static int CompareDoubleInt(DoubleInt di1, DoubleInt di2)
{
if (di1 == null)
{
if (di2 == null) return 0;
else return -1;
}
else
{
if (di2 == null) return 1;
else
{
return di1.int1.CompareTo(di2.int1);
}
}
}
|
Éliminer les doublons
Requête LINQ
|
personnes.GroupBy(p => p.PersonId)
.Select(g => g.FirstOrDefault())
.ToList();
|
Covariance
|
List<B> listB;
List<A> listA = listB;
IList<A> listA = listB;
var listA = (IList<A>)listB;
IList<B> iListB;
IList<A> iListA = iListB;
var iListA = (IList<A>)iListB;
IEnumerable<B> ieb = lb.Select(b => b);
IEnumerable<A> iea = ieb;
IReadOnlyCollection<B> lbr = lb.AsReadOnly();
IReadOnlyCollection<A> lar = lbr;
|
|
bool areEquals = myList1.SequenceEqual(myList2);
bool areEquals = myList1.Count == myList2.Count && myList1.All(myList2.Contains);
var areNotEquals = !myList1.All(myList2.Contains);
|
IEquatable
Si les éléments de la liste sont de type référence, il faut que ce type implémente IEquatable pour permettre la comparaison.
|
public class Item : IEquatable<Item>
{
public int Id { get; set; }
public string Name { get; set; }
public bool Equals(Item other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return this.Id == other.Id && this.Name == other.Name;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return this.Equals((Item)obj);
}
public override int GetHashCode()
{
unchecked
{
return (this.Id * 397) ^ this.Name.GetHashCode();
}
}
public static bool operator ==(Item left, Item right)
{
return Equals(left, right);
}
public static bool operator !=(Item left, Item right)
{
return !Equals(left, right);
}
|
LinkedList vs List
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
|
public class ReadOnlyCollection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>
{
public ReadOnlyCollection(IList<T> list) { }
}
|
ListExtensions.cs
|
public static class ListExtensions
{
public static IReadOnlyList<T> AsReadOnly<T>(this IList<T> list) => new ReadOnlyCollection<T>(list);
}
|
|
var myList = new List<string>;
IReadOnlyCollection<string> roCollection = myList.AsReadOnly();
|
ICollection to IReadOnlyCollection
 |
A class System.Collections.ObjectModel.CollectionExtensions already exists for the AddRange method |
CollectionExtensions.cs
|
public static class CollectionExtensions
{
public static IReadOnlyCollection<T> AsReadOnly<T>(this ICollection<T> collection) => new ReadOnlyCollectionAdapter<T>(collection);
private sealed class ReadOnlyCollectionAdapter<T> : IReadOnlyCollection<T>
{
private readonly ICollection<T> source;
public ReadOnlyCollectionAdapter(ICollection<T> source) => this.source = source;
public int Count => this.source.Count;
public IEnumerator<T> GetEnumerator() => this.source.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
}
public static Collection<T> AddRange<T>(this Collection<T> collection, IEnumerable<T> items) =>
System.Collections.ObjectModel.CollectionExtensions.AddRange(collection, items);
}
|
|
ICollection<string> myCollection = new List<string>;
IReadOnlyCollection<string> roCollection = myCollection.AsReadOnly();
|