SingletonList<T>
public struct SingletonList<T> : IReadOnlyList<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>, IList<T>, ICollection<T>
Represents a list with one element.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext.Collections.Specialized
{
[StructLayout(LayoutKind.Auto)]
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public struct SingletonList<[System.Runtime.CompilerServices.Nullable(2)] T> : IReadOnlyList<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>, IList<T>, ICollection<T>
{
[StructLayout(LayoutKind.Auto)]
[System.Runtime.CompilerServices.NullableContext(0)]
public struct Enumerator : IEnumerator<T>, IEnumerator, IDisposable
{
private const byte NotRequestedState = 1;
private const byte RequestedState = 2;
private byte state;
[System.Runtime.CompilerServices.Nullable(1)]
public T Current {
[IsReadOnly]
[System.Runtime.CompilerServices.NullableContext(1)]
get;
}
[System.Runtime.CompilerServices.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;
}
}
public T Item {
[IsReadOnly]
get;
set;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public T this[int index] {
[IsReadOnly]
get {
if (index != 0)
throw new IndexOutOfRangeException(ExceptionMessages.IndexShouldBeZero);
return Item;
}
set {
if (index != 0)
throw new IndexOutOfRangeException(ExceptionMessages.IndexShouldBeZero);
Item = value;
}
}
bool ICollection<T>.IsReadOnly {
[IsReadOnly]
get {
return true;
}
}
int IReadOnlyCollection<T>.Count {
[IsReadOnly]
get {
return 1;
}
}
int ICollection<T>.Count {
[IsReadOnly]
get {
return 1;
}
}
public SingletonList(T item)
{
Item = item;
}
[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)
{
if (!EqualityComparer<T>.Default.Equals(Item, item))
return -1;
return 0;
}
[IsReadOnly]
void IList<T>.Insert(int index, T item)
{
throw new NotSupportedException();
}
[IsReadOnly]
void IList<T>.RemoveAt(int index)
{
throw new NotSupportedException();
}
[IsReadOnly]
[System.Runtime.CompilerServices.NullableContext(0)]
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)
{
return new SingletonList<T>(item);
}
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);
}
}
}