ObservableCollectionExtensions
Performance helpers avoiding struct enumerator building and generally faster accessing.
using System;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
namespace NJsonSchema
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal static class ObservableCollectionExtensions
{
public static int Count<[System.Runtime.CompilerServices.Nullable(2)] T>(this ObservableCollection<T> collection, Func<T, bool> predicate)
{
int num = 0;
for (int i = 0; i < ((Collection<T>)collection).Count; i++) {
if (predicate(((Collection<T>)collection)[i]))
num++;
}
return num;
}
public static T ElementAt<[System.Runtime.CompilerServices.Nullable(2)] T>(this ObservableCollection<T> collection, int index)
{
return ((Collection<T>)collection)[index];
}
public static T First<[System.Runtime.CompilerServices.Nullable(2)] T>(this ObservableCollection<T> collection)
{
if (((Collection<T>)collection).Count > 0)
return ((Collection<T>)collection)[0];
ThrowNoMatchingElement();
return default(T);
}
public static T First<[System.Runtime.CompilerServices.Nullable(2)] T>(this ObservableCollection<T> collection, Func<T, bool> predicate)
{
for (int i = 0; i < ((Collection<T>)collection).Count; i++) {
T val = ((Collection<T>)collection)[i];
if (predicate(val))
return val;
}
ThrowNoMatchingElement();
return default(T);
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public static T FirstOrDefault<T>(this ObservableCollection<T> collection, Func<T, bool> predicate) where T : class
{
for (int i = 0; i < ((Collection<T>)collection).Count; i++) {
T val = ((Collection<T>)collection)[i];
if (predicate(val))
return val;
}
return null;
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public static T FirstOrDefault<T>(this ObservableCollection<T> collection) where T : class
{
if (((Collection<T>)collection).Count > 0)
return ((Collection<T>)collection)[0];
return null;
}
public static bool Any<[System.Runtime.CompilerServices.Nullable(2)] T>(this ObservableCollection<T> collection)
{
return ((Collection<T>)collection).Count > 0;
}
public static bool Any<[System.Runtime.CompilerServices.Nullable(2)] T>(this ObservableCollection<T> collection, Func<T, bool> predicate)
{
for (int i = 0; i < ((Collection<T>)collection).Count; i++) {
if (predicate(((Collection<T>)collection)[i]))
return true;
}
return false;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowNoMatchingElement()
{
throw new InvalidOperationException("Collection contains no matching element");
}
}
}