<PackageReference Include="Microsoft.Extensions.Primitives" Version="10.0.0-preview.5.25277.114" />

StringValues

public struct StringValues : IList<string>, ICollection<string>, IEnumerable<string>, IEnumerable, IReadOnlyList<string>, IReadOnlyCollection<string>, IEquatable<StringValues>, IEquatable<string>, IEquatable<string[]>
Represents zero/null, one, or many strings in an efficient way.
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Numerics.Hashing; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Microsoft.Extensions.Primitives { [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] [DebuggerDisplay("{ToString()}")] [DebuggerTypeProxy(typeof(StringValuesDebugView))] public readonly struct StringValues : IList<string>, ICollection<string>, IEnumerable<string>, IEnumerable, IReadOnlyList<string>, IReadOnlyCollection<string>, IEquatable<StringValues>, IEquatable<string>, IEquatable<string[]> { [System.Runtime.CompilerServices.NullableContext(0)] public struct Enumerator : IEnumerator<string>, IEnumerator, IDisposable { private readonly string[] _values; private int _index; private string _current; [System.Runtime.CompilerServices.Nullable(2)] public string Current { [System.Runtime.CompilerServices.NullableContext(2)] get { return _current; } } [System.Runtime.CompilerServices.Nullable(2)] object IEnumerator.Current { get { return _current; } } internal Enumerator(object value) { string text = value as string; if (text != null) { _values = null; _current = text; } else { _current = null; _values = Unsafe.As<string[]>(value); } _index = 0; } public Enumerator(ref StringValues values) { this = new Enumerator(values._values); } public bool MoveNext() { int index = _index; if (index < 0) return false; string[] values = _values; if (values != null) { if ((uint)index < (uint)values.Length) { _index = index + 1; _current = values[index]; return true; } _index = -1; return false; } _index = -1; return _current != null; } void IEnumerator.Reset() { throw new NotSupportedException(); } public void Dispose() { } } private sealed class StringValuesDebugView { [CompilerGenerated] private StringValues <values>P = values; [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public string[] Items { get { return <values>P.ToArray(); } } public StringValuesDebugView(StringValues values) { } } public static readonly StringValues Empty = new StringValues(Array.Empty<string>()); private readonly object _values; public int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { object values = _values; if (values == null) return 0; if (values is string) return 1; return Unsafe.As<string[]>(values).Length; } } bool ICollection<string>.IsReadOnly { get { return true; } } string IList<string>.this[int index] { get { return this[index]; } set { throw new NotSupportedException(); } } public string this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { object values = _values; string text = values as string; if (text != null) { if (index == 0) return text; } else if (values != null) { return Unsafe.As<string[]>(values)[index]; } return OutOfBounds(); } } public StringValues(string value) { _values = value; } public StringValues(string[] values) { _values = values; } public static implicit operator StringValues(string value) { return new StringValues(value); } public static implicit operator StringValues(string[] values) { return new StringValues(values); } public static implicit operator string(StringValues values) { return values.GetStringValue(); } public static implicit operator string[](StringValues value) { return value.GetArrayValue(); } [MethodImpl(MethodImplOptions.NoInlining)] private static string OutOfBounds() { return Array.Empty<string>()[0]; } [System.Runtime.CompilerServices.NullableContext(1)] public override string ToString() { return GetStringValue() ?? string.Empty; } private string GetStringValue() { object values = _values; string text = values as string; if (text != null) return text; return <GetStringValue>g__GetStringValueFromArray|19_0(values); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public string[] ToArray() { return GetArrayValue() ?? Array.Empty<string>(); } private string[] GetArrayValue() { object values = _values; string[] array = values as string[]; if (array != null) return array; if (values != null) return new string[1] { Unsafe.As<string>(values) }; return null; } int IList<string>.IndexOf(string item) { return IndexOf(item); } private int IndexOf(string item) { object values = _values; string[] array = values as string[]; if (array != null) { for (int i = 0; i < array.Length; i++) { if (string.Equals(array[i], item, StringComparison.Ordinal)) return i; } return -1; } if (values != null) { if (!string.Equals(Unsafe.As<string>(values), item, StringComparison.Ordinal)) return -1; return 0; } return -1; } bool ICollection<string>.Contains(string item) { return IndexOf(item) >= 0; } void ICollection<string>.CopyTo(string[] array, int arrayIndex) { CopyTo(array, arrayIndex); } private void CopyTo(string[] array, int arrayIndex) { object values = _values; string[] array2 = values as string[]; if (array2 != null) Array.Copy(array2, 0, array, arrayIndex, array2.Length); else if (values != null) { if (array == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); if (arrayIndex < 0) throw new ArgumentOutOfRangeException("arrayIndex"); if (array.Length - arrayIndex < 1) throw new ArgumentException("'array' is not long enough to copy all the items in the collection. Check 'arrayIndex' and 'array' length."); array[arrayIndex] = Unsafe.As<string>(values); } } void ICollection<string>.Add(string item) { throw new NotSupportedException(); } void IList<string>.Insert(int index, string item) { throw new NotSupportedException(); } bool ICollection<string>.Remove(string item) { throw new NotSupportedException(); } void IList<string>.RemoveAt(int index) { throw new NotSupportedException(); } void ICollection<string>.Clear() { throw new NotSupportedException(); } public Enumerator GetEnumerator() { return new Enumerator(_values); } IEnumerator<string> IEnumerable<string>.GetEnumerator() { return GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public static bool IsNullOrEmpty(StringValues value) { object values = value._values; if (values == null) return true; string[] array = values as string[]; if (array != null) { switch (array.LongLength) { case 0: return true; case 1: return string.IsNullOrEmpty(array[0]); default: return false; } } return string.IsNullOrEmpty(Unsafe.As<string>(values)); } public static StringValues Concat(StringValues values1, StringValues values2) { int count = values1.Count; int count2 = values2.Count; if (count == 0) return values2; if (count2 == 0) return values1; string[] array = new string[count + count2]; values1.CopyTo(array, 0); values2.CopyTo(array, count); return new StringValues(array); } public static StringValues Concat([In] [System.Runtime.CompilerServices.IsReadOnly] ref StringValues values, string value) { if (value == null) return values; int count = values.Count; if (count == 0) return new StringValues(value); string[] array = new string[count + 1]; values.CopyTo(array, 0); array[count] = value; return new StringValues(array); } public static StringValues Concat(string value, [In] [System.Runtime.CompilerServices.IsReadOnly] ref StringValues values) { if (value == null) return values; int count = values.Count; if (count == 0) return new StringValues(value); string[] array = new string[count + 1]; array[0] = value; values.CopyTo(array, 1); return new StringValues(array); } public static bool Equals(StringValues left, StringValues right) { int count = left.Count; if (count != right.Count) return false; for (int i = 0; i < count; i++) { if (left[i] != right[i]) return false; } return true; } public static bool operator ==(StringValues left, StringValues right) { return Equals(left, right); } public static bool operator !=(StringValues left, StringValues right) { return !Equals(left, right); } public bool Equals(StringValues other) { return Equals(this, other); } public static bool Equals(string left, StringValues right) { return Equals(new StringValues(left), right); } public static bool Equals(StringValues left, string right) { return Equals(left, new StringValues(right)); } public bool Equals(string other) { return Equals(this, new StringValues(other)); } public static bool Equals(string[] left, StringValues right) { return Equals(new StringValues(left), right); } public static bool Equals(StringValues left, string[] right) { return Equals(left, new StringValues(right)); } public bool Equals(string[] other) { return Equals(this, new StringValues(other)); } public static bool operator ==(StringValues left, string right) { return Equals(left, new StringValues(right)); } public static bool operator !=(StringValues left, string right) { return !Equals(left, new StringValues(right)); } public static bool operator ==(string left, StringValues right) { return Equals(new StringValues(left), right); } public static bool operator !=(string left, StringValues right) { return !Equals(new StringValues(left), right); } public static bool operator ==(StringValues left, string[] right) { return Equals(left, new StringValues(right)); } public static bool operator !=(StringValues left, string[] right) { return !Equals(left, new StringValues(right)); } public static bool operator ==(string[] left, StringValues right) { return Equals(new StringValues(left), right); } public static bool operator !=(string[] left, StringValues right) { return !Equals(new StringValues(left), right); } public static bool operator ==(StringValues left, object right) { return left.Equals(right); } public static bool operator !=(StringValues left, object right) { return !left.Equals(right); } public static bool operator ==(object left, StringValues right) { return right.Equals(left); } public static bool operator !=(object left, StringValues right) { return !right.Equals(left); } public override bool Equals(object obj) { if (obj == null) return Equals(this, Empty); string text = obj as string; if (text != null) return Equals(this, text); string[] array = obj as string[]; if (array != null) return Equals(this, array); if (obj is StringValues) { StringValues right = (StringValues)obj; return Equals(this, right); } return false; } public override int GetHashCode() { object values = _values; string[] array = values as string[]; int count; if (array != null) { if (Count == 1) { string text = this[0]; if (text == null) { count = Count; return count.GetHashCode(); } return text.GetHashCode(); } int num = 0; for (int i = 0; i < array.Length; i++) { num = HashHelpers.Combine(num, array[i]?.GetHashCode() ?? 0); } return num; } string text2 = Unsafe.As<string>(values); if (text2 == null) { count = Count; return count.GetHashCode(); } return text2.GetHashCode(); } } }