DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.0.0-beta.9" />

.NET API 420,824 bytes

 UserDataSlot<TValue>

public struct UserDataSlot<TValue> : IEquatable<UserDataSlot<TValue>>
Uniquely identifies user data which can be associated with any object.
using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace DotNext { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public readonly struct UserDataSlot<[System.Runtime.CompilerServices.Nullable(2)] TValue> : IEquatable<UserDataSlot<TValue>> { private readonly long id; private UserDataSlot(long id) { this.id = id; } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static UserDataSlot<TValue> Allocate() { return new UserDataSlot<TValue>(UserDataSlot.NewId); } [System.Runtime.CompilerServices.NullableContext(2)] [return: NotNullIfNotNull("defaultValue")] internal TValue GetUserData([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] IDictionary<long, object> storage, TValue defaultValue) { if (!storage.TryGetValue(id, out object value) || !(value is TValue)) return defaultValue; return (TValue)value; } internal bool GetUserData([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] IDictionary<long, object> storage, [MaybeNullWhen(false)] out TValue userData) { if (storage.TryGetValue(id, out object value) && value is TValue) { TValue val = userData = (TValue)value; return true; } userData = default(TValue); return false; } internal void SetUserData([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] IDictionary<long, object> storage, TValue userData) { if (id == 0) throw new ArgumentException(ExceptionMessages.InvalidUserDataSlot); storage[id] = userData; } internal bool RemoveUserData([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] IDictionary<long, object> storage) { return storage.Remove(id); } internal bool RemoveUserData([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] Dictionary<long, object> storage, [MaybeNullWhen(false)] out TValue userData) { if (storage.Remove(id, out object value) && value is TValue) { TValue val = userData = (TValue)value; return true; } userData = default(TValue); return false; } public bool Equals([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] UserDataSlot<TValue> other) { return id == other.id; } [System.Runtime.CompilerServices.NullableContext(2)] public override bool Equals([NotNullWhen(true)] object other) { if (other is UserDataSlot<TValue>) { UserDataSlot<TValue> other2 = (UserDataSlot<TValue>)other; return Equals(other2); } return false; } public override int GetHashCode() { return id.GetHashCode(); } public override string ToString() { return id.ToString((IFormatProvider)null); } public static bool operator ==([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] UserDataSlot<TValue> first, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] UserDataSlot<TValue> second) { return first.id == second.id; } public static bool operator !=([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] UserDataSlot<TValue> first, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] UserDataSlot<TValue> second) { return first.id != second.id; } } }