DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="5.3.0" />

 SingletonList<T>

Represents a list with one element.
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace DotNext.Collections.Specialized { [StructLayout(LayoutKind.Auto)] [RequiredMember] public struct SingletonList<[Nullable(2)] T> : IReadOnlyList<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>, IList<T>, ICollection<T>, ITuple, IReadOnlySet<T> { [StructLayout(LayoutKind.Auto)] public struct Enumerator : IEnumerator<T>, IEnumerator, IDisposable { private const byte NotRequestedState = 1; private const byte RequestedState = 2; private byte state; [Nullable(1)] public T Current { [IsReadOnly] [NullableContext(1)] get; } [Nullable(2)] object IEnumerator.Current { [IsReadOnly] get { return Current; } } internal Enumerator(T item) { Current = item; state = 1; } void IDisposable.Dispose() { this = default(Enumerator); } public bool MoveNext() { if (state == 1) { state = 2; return true; } return false; } public void Reset() { if (state == 2) state = 1; } } [Nullable(1)] [RequiredMember] public T Item; [Nullable(1)] [EditorBrowsable(EditorBrowsableState.Never)] public T this[int index] { [IsReadOnly] [NullableContext(1)] get { ArgumentOutOfRangeException.ThrowIfNotEqual<int>(index, 0, "index"); return Item; } [NullableContext(1)] set { ArgumentOutOfRangeException.ThrowIfNotEqual<int>(index, 0, "index"); Item = value; } } [Nullable(2)] object ITuple.this[int index] { [IsReadOnly] get { return this[index]; } } int ITuple.Length { [IsReadOnly] get { return 1; } } bool ICollection<T>.IsReadOnly { [IsReadOnly] get { return true; } } int IReadOnlyCollection<T>.Count { [IsReadOnly] get { return 1; } } int ICollection<T>.Count { [IsReadOnly] get { return 1; } } [IsReadOnly] void ICollection<T>.Clear() { throw new NotSupportedException(); } [IsReadOnly] bool ICollection<T>.Contains(T item) { return EqualityComparer<T>.Default.Equals(Item, item); } [IsReadOnly] void ICollection<T>.CopyTo(T[] array, int arrayIndex) { MemoryExtensions.AsSpan<T>(array, arrayIndex)[0] = Item; } [IsReadOnly] bool ICollection<T>.Remove(T item) { throw new NotSupportedException(); } [IsReadOnly] void ICollection<T>.Add(T item) { throw new NotSupportedException(); } [IsReadOnly] int IList<T>.IndexOf(T item) { return Unsafe.BitCast<bool, byte>(EqualityComparer<T>.Default.Equals(Item, item)) - 1; } [IsReadOnly] void IList<T>.Insert(int index, T item) { throw new NotSupportedException(); } [IsReadOnly] void IList<T>.RemoveAt(int index) { throw new NotSupportedException(); } [IsReadOnly] public Enumerator GetEnumerator() { return new Enumerator(Item); } [IsReadOnly] IEnumerator<T> IEnumerable<T>.GetEnumerator() { return GetEnumerator(); } [IsReadOnly] IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public static implicit operator SingletonList<T>(T item) { SingletonList<T> result = default(SingletonList<T>); result.Item = item; return result; } [IsReadOnly] bool IReadOnlySet<T>.Contains(T item) { return EqualityComparer<T>.Default.Equals(Item, item); } [IsReadOnly] bool IReadOnlySet<T>.Overlaps(IEnumerable<T> other) { return Enumerable.Contains<T>(other, Item); } [IsReadOnly] bool IReadOnlySet<T>.SetEquals(IEnumerable<T> other) { int num = default(int); if (Enumerable.TryGetNonEnumeratedCount<T>(other, ref num)) { if (num == 1) return Enumerable.Contains<T>(other, Item); return false; } using (IEnumerator<T> enumerator = other.GetEnumerator()) return enumerator.MoveNext() && EqualityComparer<T>.Default.Equals(Item, enumerator.Current) && !enumerator.MoveNext(); } [IsReadOnly] bool IReadOnlySet<T>.IsSubsetOf(IEnumerable<T> other) { return Enumerable.Contains<T>(other, Item); } [IsReadOnly] bool IReadOnlySet<T>.IsProperSubsetOf(IEnumerable<T> other) { int num = default(int); if (Enumerable.TryGetNonEnumeratedCount<T>(other, ref num)) { if (num > 1) return Enumerable.Contains<T>(other, Item); return false; } bool flag = false; foreach (T item in other) { if (flag) return true; if (EqualityComparer<T>.Default.Equals(Item, item)) flag = true; } return false; } [IsReadOnly] bool IReadOnlySet<T>.IsProperSupersetOf(IEnumerable<T> other) { int num = default(int); if (Enumerable.TryGetNonEnumeratedCount<T>(other, ref num)) return num == 0; using (IEnumerator<T> enumerator = other.GetEnumerator()) return !enumerator.MoveNext(); } [IsReadOnly] bool IReadOnlySet<T>.IsSupersetOf(IEnumerable<T> other) { int num = default(int); if (Enumerable.TryGetNonEnumeratedCount<T>(other, ref num)) { switch (num) { case 1: return Enumerable.Contains<T>(other, Item); default: return false; case 0: return true; } } using (IEnumerator<T> enumerator = other.GetEnumerator()) return !enumerator.MoveNext() || (EqualityComparer<T>.Default.Equals(Item, enumerator.Current) && !enumerator.MoveNext()); } T get_Item(int index) { return this[index]; } T IReadOnlyList<!0>.get_Item(int index) { return this.get_Item(index); } T get_Item(int index) { return this[index]; } T IList<!0>.get_Item(int index) { return this.get_Item(index); } void set_Item(int index, T value) { this[index] = value; } void IList<!0>.set_Item(int index, T value) { this.set_Item(index, value); } } }