Sprache: C#
ObservableCollection extension methods in .Net
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace CollectionExtensions
{
public static class ObservableCollectionExtension
{
public static void AddIfNotExists<T>(this ObservableCollection<T> collection, T value)
{
CheckObservableCollectionIsNull(collection);
if (!collection.Contains(value))
collection.Add(value);
}
public static void UpdateValue<T>(this ObservableCollection<T> collection, T value, T newValue)
{
CheckObservableCollectionAndValueIsNull(collection, value);
CheckValueIsNull(newValue);
var index = collection.IndexOf(value);
collection[index] = newValue;
}
public static void DeleteIfExists<T>(this ObservableCollection<T> collection, T value)
{
CheckObservableCollectionAndValueIsNull(collection, value);
if (collection.Contains(value))
collection.Remove(value);
}
public static bool AreValuesEmpty<T>(this ObservableCollection<T> collection)
{
CheckObservableCollectionIsNull(collection);
return collection.All(x => x == null);
}
private static void CheckObservableCollectionAndValueIsNull<T>(this ObservableCollection<T> collection, T value)
{
CheckObservableCollectionIsNull(collection);
CheckValueIsNull(value);
}
private static void CheckValueIsNull<T>(T value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
}
private static void CheckObservableCollectionIsNull<T>(this ObservableCollection<T> collection)
{
if (collection == null) throw new ArgumentNullException(nameof(collection));
}
}
}
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace CollectionExtensions
{
public static class ObservableCollectionExtension
{
public static void AddIfNotExists<T>(this ObservableCollection<T> collection, T value)
{
CheckObservableCollectionIsNull(collection);
if (!collection.Contains(value))
collection.Add(value);
}
public static void UpdateValue<T>(this ObservableCollection<T> collection, T value, T newValue)
{
CheckObservableCollectionAndValueIsNull(collection, value);
CheckValueIsNull(newValue);
var index = collection.IndexOf(value);
collection[index] = newValue;
}
public static void DeleteIfExists<T>(this ObservableCollection<T> collection, T value)
{
CheckObservableCollectionAndValueIsNull(collection, value);
if (collection.Contains(value))
collection.Remove(value);
}
public static bool AreValuesEmpty<T>(this ObservableCollection<T> collection)
{
CheckObservableCollectionIsNull(collection);
return collection.All(x => x == null);
}
private static void CheckObservableCollectionAndValueIsNull<T>(this ObservableCollection<T> collection, T value)
{
CheckObservableCollectionIsNull(collection);
CheckValueIsNull(value);
}
private static void CheckValueIsNull<T>(T value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
}
private static void CheckObservableCollectionIsNull<T>(this ObservableCollection<T> collection)
{
if (collection == null) throw new ArgumentNullException(nameof(collection));
}
}
}
Alte URL:
/snippet/observablecollection-extension-methods-in-net/15165
Da [b]ObservableCollection[/b] von [b]Collection [/b] erbt, welches wiederum [b]IList [/b] implementiert, würde es auch reichen die Methoden deines anderen Snippets zu verwenden.
https://dotnet-snippets.de/snippet/list-extension-methods-in-net/15164
Ansonsten gilt das selbe wie ich unter dem anderen Snippet postete.
Das habe ich nicht bedacht, da hast du natürlich auch wieder Recht 😉