Skip to content

fedandburk/net-common-extensions

Repository files navigation

Extensions for .NET

GitHub Nuget CI CD CodeFactor

Installation

Use NuGet package manager to install this library.

Install-Package Fedandburk.Common.Extensions

Usage

using Fedandburk.Common.Extensions;

ICommand Extensions

To safely check whether the ICommand can be executed in its current state:

var canExecute = Command.SafeCanExecute(parameter);

To safely invoke the ICommand if this command can be executed in its current state:

var isExecuted = Command.SafeExecute(parameter);

IEnumerable Extensions

To transform a two-dimensional collection or an ObservableCollection into a single-dimensional collection and keep the CollectionChanged events:

Items = new ObservableCollection<ObservableCollection<int>>
{
    new ObservableCollection<int> { 1, 2 },
    new ObservableCollection<int> { 3 }
};

IEnumerable<int> flatItems = Items.ObservableFlatten();

To transform each element of a collection into a new form and keep the CollectionChanged events:

Items = new ObservableCollection<int> {1, 2, 3, 4, 5, 6};

IEnumerable<string> wrappedItems = Items.ObservableSelect(item => item.ToString());

To search for the specified object and get the index of its first occurrence in a collection:

var index = default(IEnumerable).IndexOf(item);
var index = default(IEnumerable).IndexOf(item, comparer);
var index = default(IEnumerable<TItem>).IndexOf(item);
var index = default(IEnumerable<<TItem>>).IndexOf(item, comparer);

To get a specified number of contiguous elements from the specified index:

var items = default(IEnumerable<TItem>).Take(startIndex, length);

To check if the specified enumerable is null or has a length of zero:

var isNullOrEmpty = default(IEnumerable).IsNullOrEmpty();

To get the number of elements in a sequence:

var count = default(IEnumerable).Count();

To compute the sum of a sequence of TimeSpan values:

var sum = new[] {100L, 1000L, 10000L}.Sum(TimeSpan.FromTicks);
var sum = new[] {TimeSpan.FromDays(1), TimeSpan.FromHours(2)}.Sum();

INotifyCollectionChanged Extensions

To safely subscribe to the CollectionChanged event of an INotifyCollectionChanged-based collection:

Items = new ObservableCollection { 1, 2, 3 };

void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args) {}

IDisposable subscription = Items.WeakSubscribe(OnCollectionChanged);

String Extensions

To check is the specified string is null or an empty string:

var isNullOrEmpty = "string".IsNullOrEmpty();

To check is the specified string is null, empty, or consists only of white-space characters:

var isNullOrWhiteSpace = "string".IsNullOrWhiteSpace();

About

A collection of extensions for .NET.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

Generated from fedandburk/template-net