« Les méthodes d'extension » : différence entre les versions
Apparence
(11 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 25 : | Ligne 25 : | ||
= Useful extension methods = | = Useful extension methods = | ||
== CollectionExtension == | == CollectionExtension == | ||
<filebox fn="CollectionExtension. | <filebox fn="CollectionExtension.cs" collapsed> | ||
/// <summary> | /// <summary> | ||
/// Adds each item to the list. | /// Adds each item to the list. | ||
Ligne 36 : | Ligne 36 : | ||
public static void AddEach<T>(this ICollection<T> collection, IEnumerable<T> elements) | public static void AddEach<T>(this ICollection<T> collection, IEnumerable<T> elements) | ||
{ | { | ||
ArgumentNullException.ThrowIfNull(collection); | |||
ArgumentNullException.ThrowIfNull(elements); | |||
if (collection is List<T> list) | if (collection is List<T> list) | ||
Ligne 67 : | Ligne 60 : | ||
public static void RemoveEach<T>(this ICollection<T> collection, IEnumerable<T> elements) | public static void RemoveEach<T>(this ICollection<T> collection, IEnumerable<T> elements) | ||
{ | { | ||
ArgumentNullException.ThrowIfNull(collection); | |||
ArgumentNullException.ThrowIfNull(elements); | |||
foreach (var element in elements) | foreach (var element in elements) | ||
Ligne 85 : | Ligne 71 : | ||
public static void Remove<T>(this ICollection<T> collection, Func<T, bool> predicate) | public static void Remove<T>(this ICollection<T> collection, Func<T, bool> predicate) | ||
{ | { | ||
ArgumentNullException.ThrowIfNull(collection); | |||
collection.RemoveEach(collection.Where(predicate)); | collection.RemoveEach(collection.Where(predicate)); | ||
Ligne 95 : | Ligne 78 : | ||
public static void RemoveSingle<T>(this ICollection<T> collection, Func<T, bool> predicate) | public static void RemoveSingle<T>(this ICollection<T> collection, Func<T, bool> predicate) | ||
{ | { | ||
ArgumentNullException.ThrowIfNull(collection); | |||
collection.Remove(collection.Single(predicate)); | collection.Remove(collection.Single(predicate)); | ||
} | } | ||
</ | </filebox> | ||
== IEnumerableExtension == | == IEnumerableExtension == | ||
<filebox fn='IEnumerableExtension.cs' collapsed> | <filebox fn='IEnumerableExtension.cs' collapsed> | ||
/// <summary> | |||
/// Update the property value of the items of the collection. | |||
/// </summary> | |||
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) | |||
{ | |||
foreach (T item in source) | |||
action(item); | |||
} | |||
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action) | |||
{ | |||
foreach (T item in source) | |||
{ | |||
action(item); | |||
yield return item; | |||
} | |||
} | |||
/// <summary> | /// <summary> | ||
/// Sorts the elements of a sequence according to an element's property. | /// Sorts the elements of a sequence according to an element's property. | ||
Ligne 115 : | Ligne 113 : | ||
{ | { | ||
var propertyDescriptor = TypeDescriptor.GetProperties(typeof(T)) | var propertyDescriptor = TypeDescriptor.GetProperties(typeof(T)) | ||
.Find(propertyName, false); | |||
if (propertyDescriptor == null) | if (propertyDescriptor == null) | ||
Ligne 123 : | Ligne 121 : | ||
source.OrderBy(x => propertyDescriptor.GetValue(x)) : | source.OrderBy(x => propertyDescriptor.GetValue(x)) : | ||
source.OrderByDescending(x => propertyDescriptor.GetValue(x)); | source.OrderByDescending(x => propertyDescriptor.GetValue(x)); | ||
} | |||
</filebox> | |||
== IListExtension == | |||
<filebox fn='IListExtension.cs' collapsed> | |||
/// <summary> | |||
/// Groups items by range, allowing overlap. | |||
/// Groups by a range of 10 items. Group 1 will get items 0 to 10. Group 2 will get items 10 to 20. | |||
/// </summary> | |||
public static IEnumerable<IReadOnlyCollection<T>> GroupByRange<T>(this List<T> source, int rangeSize) | |||
{ | |||
var result = new Dictionary<int, List<T>>(); | |||
for (var i = 0; i < source.Count; i++) | |||
{ | |||
var groupId = i / rangeSize; | |||
if (result.TryGetValue(groupId, out var list)) | |||
list.Add(source[i]); | |||
else | |||
result[groupId] = [source[i]]; | |||
if (i % rangeSize == 0 && i - 1 > 0) | |||
result[groupId - 1].Add(source[i]); | |||
} | |||
return result.Values.Select(x => x.AsReadOnly()); | |||
} | } | ||
</filebox> | </filebox> |
Dernière version du 1 novembre 2024 à 14:41
Disponible à partir de C# 3, elles permettent d'ajouter des méthodes à une classe ou à une structure sans en modifier son code.
![]() |
|
// Déclaration de la méthode d'extension dans une classe static
public static class StringExtensions
{
/// <summary>
/// A Contains method with a comparison type.
/// </summary>
public static bool Contains(this string source, string value, StringComparison comparisonType)
{
return source.IndexOf(value, comparisonType) >= 0;
}
}
// Utilisation de la méthode d'extension : comparaison de string en ignorant la casse
var monString = "ASTRINGTOTEST";
monString.Contains("string", StringComparison.OrdinalIgnoreCase);
|
Useful extension methods
CollectionExtension
CollectionExtension.cs |
IEnumerableExtension
IEnumerableExtension.cs |
IListExtension
IListExtension.cs |