public static ReadOnlyCollection<T> ToReadOnlyCollection<T>(this IEnumerable<T> enumeration)
{
- return new ReadOnlyCollection<T>(enumeration.ToArray());
+ return Array.AsReadOnly(enumeration.ToArray());
}
public static MethodInfo FilterAccessor(this MethodInfo accessor, bool nonPublic)
if (count == 0)
{
- // TODO https://github.com/dotnet/runtime/issues/76028: Replace with ReadOnlyCollection<TKey>.Empty.
- return Array.Empty<TKey>().AsReadOnly();
+ return ReadOnlyCollection<TKey>.Empty;
}
var keys = new TKey[count];
if (count == 0)
{
- // TODO https://github.com/dotnet/runtime/pull/76097: Replace with ReadOnlyCollection<TValue>.Empty.
- return Array.Empty<TValue>().AsReadOnly();
+ return ReadOnlyCollection<TValue>.Empty;
}
var values = new TValue[count];
{
ArgumentNullException.ThrowIfNull(source);
- return new ReadOnlyCollection<T>(source.AsArray());
+ return Array.AsReadOnly(source.AsArray());
}
public static IEnumerable<T>? ConcatAllowingNull<T>(this IEnumerable<T>? source, IEnumerable<T>? second)
: base(message, innerException)
{
Requires.NullOrNotNullElements(errors, nameof(errors));
- _errors = new ReadOnlyCollection<CompositionError>(errors == null ? Array.Empty<CompositionError>() : errors.ToArray<CompositionError>());
+ _errors = Array.AsReadOnly(errors == null ? Array.Empty<CompositionError>() : errors.ToArray<CompositionError>());
}
/// <summary>
}
_providers = copiedProviders;
- _readOnlyProviders = new ReadOnlyCollection<ExportProvider>(_providers);
+ _readOnlyProviders = Array.AsReadOnly(_providers);
}
/// <summary>
_rootProvider.ExportsChanged += OnExportsChangedInternal;
_rootProvider.ExportsChanging += OnExportsChangingInternal;
- _providers = (providers != null) ? new ReadOnlyCollection<ExportProvider>((ExportProvider[])providers.Clone()) : EmptyProviders;
+ _providers = (providers != null) ? Array.AsReadOnly((ExportProvider[])providers.Clone()) : EmptyProviders;
}
internal CompositionOptions CompositionOptions
_catalogCollection.Remove(catalogToRemove.Item2);
}
- _loadedFiles = afterFiles.ToReadOnlyCollection();
+ _loadedFiles = Array.AsReadOnly(afterFiles);
// Lastly complete any changes added to the atomicComposition during the change event
atomicComposition.Complete();
_assemblyCatalogs = new Dictionary<string, AssemblyCatalog>();
_catalogCollection = new ComposablePartCatalogCollection(null, null, null);
- _loadedFiles = GetFiles().ToReadOnlyCollection();
+ _loadedFiles = Array.AsReadOnly(GetFiles());
foreach (string file in _loadedFiles)
{
/// <summary>
/// Boundaries implemented by the created ExportLifetimeContext{T}s.
/// </summary>
- public ReadOnlyCollection<string> SharingBoundaryNames => new ReadOnlyCollection<string>(_sharingBoundaryNames);
+ public ReadOnlyCollection<string> SharingBoundaryNames => Array.AsReadOnly(_sharingBoundaryNames);
}
}
<Compile Include="System\Runtime\CompilerServices\TrueReadOnlyCollection.cs" />
<Compile Include="System\Dynamic\Utils\CachedReflectionInfo.cs" />
<Compile Include="System\Dynamic\Utils\CollectionExtensions.cs" />
- <Compile Include="System\Dynamic\Utils\EmptyReadOnlyCollection.cs" />
<Compile Include="System\Dynamic\UpdateDelegates.Generated.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
/// </summary>
public static ReadOnlyCollection<T> ToReadOnly<T>(this IEnumerable<T>? enumerable)
{
- if (enumerable == null)
+ if (enumerable != null && enumerable != ReadOnlyCollection<T>.Empty)
{
- return EmptyReadOnlyCollection<T>.Instance;
- }
+ if (enumerable is TrueReadOnlyCollection<T> troc)
+ {
+ return troc;
+ }
- if (enumerable is TrueReadOnlyCollection<T> troc)
- {
- return troc;
- }
+ if (enumerable is ReadOnlyCollectionBuilder<T> builder)
+ {
+ return builder.ToReadOnlyCollection();
+ }
- if (enumerable is ReadOnlyCollectionBuilder<T> builder)
- {
- return builder.ToReadOnlyCollection();
+ T[] array = enumerable.ToArray();
+ if (array.Length != 0)
+ {
+ return new TrueReadOnlyCollection<T>(array);
+ }
}
- T[] array = enumerable.ToArray();
- return array.Length == 0 ?
- EmptyReadOnlyCollection<T>.Instance :
- new TrueReadOnlyCollection<T>(array);
+ return ReadOnlyCollection<T>.Empty;
}
// We could probably improve the hashing here
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Collections.ObjectModel;
-using System.Runtime.CompilerServices;
-
-namespace System.Dynamic.Utils
-{
- internal static class EmptyReadOnlyCollection<T>
- {
- public static readonly ReadOnlyCollection<T> Instance = new TrueReadOnlyCollection<T>(Array.Empty<T>());
- }
-}
internal virtual ReadOnlyCollection<ParameterExpression> GetOrMakeVariables()
{
- return EmptyReadOnlyCollection<ParameterExpression>.Instance;
+ return ReadOnlyCollection<ParameterExpression>.Empty;
}
/// <summary>
/// <returns>The created <see cref="BlockExpression"/>.</returns>
public static BlockExpression Block(IEnumerable<Expression> expressions)
{
- return Block(EmptyReadOnlyCollection<ParameterExpression>.Instance, expressions);
+ return Block(ReadOnlyCollection<ParameterExpression>.Empty, expressions);
}
/// <summary>
/// <returns>The created <see cref="BlockExpression"/>.</returns>
public static BlockExpression Block(Type type, IEnumerable<Expression> expressions)
{
- return Block(type, EmptyReadOnlyCollection<ParameterExpression>.Instance, expressions);
+ return Block(type, ReadOnlyCollection<ParameterExpression>.Empty, expressions);
}
/// <summary>
{
return expressions.Count switch
{
- 0 => BlockCore(typeof(void), EmptyReadOnlyCollection<ParameterExpression>.Instance, EmptyReadOnlyCollection<Expression>.Instance),
+ 0 => BlockCore(typeof(void), ReadOnlyCollection<ParameterExpression>.Empty, ReadOnlyCollection<Expression>.Empty),
2 => new Block2(expressions[0], expressions[1]),
3 => new Block3(expressions[0], expressions[1], expressions[2]),
4 => new Block4(expressions[0], expressions[1], expressions[2], expressions[3]),
vars = vars.AddFirst(parent.SelfVariable);
}
- Dictionary<Expression, int> indexes = new Dictionary<Expression, int>(vars.Count);
- for (int i = 0; i < vars.Count; i++)
+ if (vars.Count != 0)
{
- indexes.Add(vars[i], i);
+ Dictionary<Expression, int> indexes = new Dictionary<Expression, int>(vars.Count);
+ for (int i = 0; i < vars.Count; i++)
+ {
+ indexes.Add(vars[i], i);
+ }
+
+ Indexes = new ReadOnlyDictionary<Expression, int>(indexes);
+ }
+ else
+ {
+ Indexes = ReadOnlyDictionary<Expression, int>.Empty;
}
- SelfVariable = Expression.Variable(typeof(object[]), name: null);
Parent = parent;
Variables = vars;
- Indexes = new ReadOnlyDictionary<Expression, int>(indexes);
+ SelfVariable = Expression.Variable(typeof(object[]), name: null);
}
internal ParameterExpression? ParentVariable => Parent?.SelfVariable;
internal override ReadOnlyCollection<Expression> GetOrMakeArguments()
{
- return EmptyReadOnlyCollection<Expression>.Instance;
+ return ReadOnlyCollection<Expression>.Empty;
}
public override Expression GetArgument(int index)
throw Error.ArgumentOutOfRange(nameof(index));
}
- internal override ReadOnlyCollection<ParameterExpression> GetOrMakeParameters() => EmptyReadOnlyCollection<ParameterExpression>.Instance;
+ internal override ReadOnlyCollection<ParameterExpression> GetOrMakeParameters() => ReadOnlyCollection<ParameterExpression>.Empty;
internal override Expression<TDelegate> Rewrite(Expression body, ParameterExpression[]? parameters)
{
ReadOnlyCollection<Expression> initializerlist = initializers.ToReadOnly();
if (initializerlist.Count == 0)
{
- return new ListInitExpression(newExpression, EmptyReadOnlyCollection<ElementInit>.Instance);
+ return new ListInitExpression(newExpression, ReadOnlyCollection<ElementInit>.Empty);
}
MethodInfo? addMethod = FindMethod(newExpression.Type, "Add", null, new Expression[] { initializerlist[0] }, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
internal override ReadOnlyCollection<Expression> GetOrMakeArguments()
{
- return EmptyReadOnlyCollection<Expression>.Instance;
+ return ReadOnlyCollection<Expression>.Empty;
}
internal override bool SameArguments(ICollection<Expression>? arguments) =>
internal override ReadOnlyCollection<Expression> GetOrMakeArguments()
{
- return EmptyReadOnlyCollection<Expression>.Instance;
+ return ReadOnlyCollection<Expression>.Empty;
}
internal override bool SameArguments(ICollection<Expression>? arguments) =>
{
throw Error.TypeMissingDefaultConstructor(type, nameof(type));
}
- return new NewValueTypeExpression(type, EmptyReadOnlyCollection<Expression>.Instance, null);
+ return new NewValueTypeExpression(type, ReadOnlyCollection<Expression>.Empty, null);
}
[RequiresUnreferencedCode(PropertyFromAccessorRequiresUnreferencedCode)]
/// <returns>A new instance of <see cref="ReadOnlyCollection{T}"/>.</returns>
public ReadOnlyCollection<T> ToReadOnlyCollection()
{
+ if (_size == 0)
+ {
+ return ReadOnlyCollection<T>.Empty;
+ }
+
// Can we use the stored array?
T[] items;
if (_size == _items.Length)
var sourceList = new ReadOnlyCollection<int>(sourceIntegers);
var convertedList = new ReadOnlyCollection<string>(convertedStrings);
- var emptyIntegersList = new ReadOnlyCollection<int>(Array.Empty<int>());
- var emptyStringsList = new ReadOnlyCollection<string>(Array.Empty<string>());
-
Assert.Equal(convertedList, sourceList.Select(i => i.ToString()).ToList());
Assert.Equal(sourceList, sourceList.Where(i => true).ToList());
- Assert.Equal(emptyIntegersList, sourceList.Where(i => false).ToList());
+ Assert.Equal(ReadOnlyCollection<int>.Empty, sourceList.Where(i => false).ToList());
Assert.Equal(convertedList, sourceList.Where(i => true).Select(i => i.ToString()).ToList());
- Assert.Equal(emptyStringsList, sourceList.Where(i => false).Select(i => i.ToString()).ToList());
+ Assert.Equal(ReadOnlyCollection<string>.Empty, sourceList.Where(i => false).Select(i => i.ToString()).ToList());
Assert.Equal(convertedList, sourceList.Select(i => i.ToString()).Where(s => s != null).ToList());
- Assert.Equal(emptyStringsList, sourceList.Select(i => i.ToString()).Where(s => s == null).ToList());
+ Assert.Equal(ReadOnlyCollection<string>.Empty, sourceList.Select(i => i.ToString()).Where(s => s == null).ToList());
}
[Fact]
public partial class ReadOnlyObservableCollection<T> : System.Collections.ObjectModel.ReadOnlyCollection<T>, System.Collections.Specialized.INotifyCollectionChanged, System.ComponentModel.INotifyPropertyChanged
{
public ReadOnlyObservableCollection(System.Collections.ObjectModel.ObservableCollection<T> list) : base (default(System.Collections.Generic.IList<T>)) { }
+ public static new System.Collections.ObjectModel.ReadOnlyObservableCollection<T> Empty { get { throw null; } }
protected virtual event System.Collections.Specialized.NotifyCollectionChangedEventHandler? CollectionChanged { add { } remove { } }
protected virtual event System.ComponentModel.PropertyChangedEventHandler? PropertyChanged { add { } remove { } }
event System.Collections.Specialized.NotifyCollectionChangedEventHandler? System.Collections.Specialized.INotifyCollectionChanged.CollectionChanged { add { } remove { } }
((INotifyPropertyChanged)Items).PropertyChanged += new PropertyChangedEventHandler(HandlePropertyChanged);
}
+ /// <summary>Gets an empty <see cref="ReadOnlyObservableCollection{T}"/>.</summary>
+ /// <value>An empty <see cref="ReadOnlyObservableCollection{T}"/>.</value>
+ /// <remarks>The returned instance is immutable and will always be empty.</remarks>
+ public static new ReadOnlyObservableCollection<T> Empty { get; } = new ReadOnlyObservableCollection<T>(new ObservableCollection<T>());
+
/// <summary>
/// CollectionChanged event (per <see cref="INotifyCollectionChanged" />).
/// </summary>
AssertExtensions.Throws<ArgumentNullException>("dictionary", () => new ReadOnlyDictionary<int, string>(null));
}
+ [Fact]
+ public static void Empty_Idempotent()
+ {
+ Assert.NotNull(ReadOnlyDictionary<string, int>.Empty);
+ Assert.Equal(0, ReadOnlyDictionary<string, int>.Empty.Count);
+ Assert.Same(ReadOnlyDictionary<string, int>.Empty, ReadOnlyDictionary<string, int>.Empty);
+ }
+
/// <summary>
/// Tests that true is returned when the key exists in the dictionary
/// and false otherwise.
}
[Fact]
+ public static void Empty_Idempotent()
+ {
+ Assert.NotNull(ReadOnlyObservableCollection<int>.Empty);
+ Assert.Equal(0, ReadOnlyObservableCollection<int>.Empty.Count);
+ Assert.Same(ReadOnlyObservableCollection<int>.Empty, ReadOnlyObservableCollection<int>.Empty);
+ }
+
+ [Fact]
public static void GetItemTests()
{
string[] anArray = new string[] { "one", "two", "three", "four", "five" };
this.list = list;
}
- // TODO https://github.com/dotnet/runtime/issues/76028: Make this public.
/// <summary>Gets an empty <see cref="ReadOnlyCollection{T}"/>.</summary>
+ /// <value>An empty <see cref="ReadOnlyCollection{T}"/>.</value>
/// <remarks>The returned instance is immutable and will always be empty.</remarks>
- internal static ReadOnlyCollection<T> Empty { get; } = new ReadOnlyCollection<T>(Array.Empty<T>());
+ public static ReadOnlyCollection<T> Empty { get; } = new ReadOnlyCollection<T>(Array.Empty<T>());
public int Count => list.Count;
m_dictionary = dictionary;
}
+ /// <summary>Gets an empty <see cref="ReadOnlyDictionary{TKey, TValue}"/>.</summary>
+ /// <value>An empty <see cref="ReadOnlyDictionary{TKey, TValue}"/>.</value>
+ /// <remarks>The returned instance is immutable and will always be empty.</remarks>
+ public static ReadOnlyDictionary<TKey, TValue> Empty { get; } = new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>());
+
protected IDictionary<TKey, TValue> Dictionary => m_dictionary;
public KeyCollection Keys => _keys ??= new KeyCollection(m_dictionary.Keys);
set => _helper.Members = value;
}
- public override ReadOnlyCollection<DataMember> DataMembers => (Members == null) ? DataContract.s_emptyDataMemberList : Members.AsReadOnly();
+ public override ReadOnlyCollection<DataMember> DataMembers => (Members == null) ? ReadOnlyCollection<DataMember>.Empty : Members.AsReadOnly();
internal XmlDictionaryString?[]? ChildElementNamespaces
{
DynamicallyAccessedMemberTypes.PublicFields |
DynamicallyAccessedMemberTypes.PublicProperties;
- internal static ReadOnlyCollection<DataMember> s_emptyDataMemberList = new List<DataMember>().AsReadOnly();
-
private XmlDictionaryString _name;
private XmlDictionaryString _ns;
private readonly DataContractCriticalHelper _helper;
return false;
}
- public virtual ReadOnlyCollection<DataMember> DataMembers => s_emptyDataMemberList;
+ public virtual ReadOnlyCollection<DataMember> DataMembers => ReadOnlyCollection<DataMember>.Empty;
internal virtual void WriteRootElement(XmlWriterDelegator writer, XmlDictionaryString name, XmlDictionaryString? ns)
{
{
get
{
- if (_knownTypeCollection == null)
- {
- if (_knownTypeList != null)
- {
- _knownTypeCollection = new ReadOnlyCollection<Type>(_knownTypeList);
- }
- else
- {
- _knownTypeCollection = new ReadOnlyCollection<Type>(Type.EmptyTypes);
- }
- }
- return _knownTypeCollection;
+ return _knownTypeCollection ??=
+ _knownTypeList != null ?
+ new ReadOnlyCollection<Type>(_knownTypeList) :
+ ReadOnlyCollection<Type>.Empty;
}
}
set => _helper.Members = value;
}
- public override ReadOnlyCollection<DataMember> DataMembers => (Members == null) ? DataContract.s_emptyDataMemberList : Members.AsReadOnly();
+ public override ReadOnlyCollection<DataMember> DataMembers => (Members == null) ? ReadOnlyCollection<DataMember>.Empty : Members.AsReadOnly();
internal List<long>? Values
{
{
get
{
- if (_knownTypeCollection == null)
- {
- if (knownTypeList != null)
- {
- _knownTypeCollection = new ReadOnlyCollection<Type>(knownTypeList);
- }
- else
- {
- _knownTypeCollection = new ReadOnlyCollection<Type>(Type.EmptyTypes);
- }
- }
- return _knownTypeCollection;
+ return _knownTypeCollection ??=
+ knownTypeList != null ?
+ new ReadOnlyCollection<Type>(knownTypeList) :
+ ReadOnlyCollection<Type>.Empty;
}
}
public static ReadOnlyCollection<CustomAttributeTypedArgument> CloneForApiReturn(this IList<CustomAttributeTypedArgument> cats)
{
int count = cats.Count;
- CustomAttributeTypedArgument[] clones = new CustomAttributeTypedArgument[count];
+ CustomAttributeTypedArgument[] clones = count != 0 ? new CustomAttributeTypedArgument[count] : Array.Empty<CustomAttributeTypedArgument>();
for (int i = 0; i < count; i++)
{
clones[i] = cats[i].CloneForApiReturn();
}
- return clones.ToReadOnlyCollection();
+ return Array.AsReadOnly(clones);
}
/// <summary>
public static ReadOnlyCollection<CustomAttributeNamedArgument> CloneForApiReturn(this IList<CustomAttributeNamedArgument> cans)
{
int count = cans.Count;
- CustomAttributeNamedArgument[] clones = new CustomAttributeNamedArgument[count];
+ CustomAttributeNamedArgument[] clones = count != 0 ? new CustomAttributeNamedArgument[count] : Array.Empty<CustomAttributeNamedArgument>();
for (int i = 0; i < count; i++)
{
clones[i] = cans[i].CloneForApiReturn();
}
- return clones.ToReadOnlyCollection();
+ return Array.AsReadOnly(clones);
}
/// <summary>
return cat;
int count = cats.Count;
- CustomAttributeTypedArgument[] cads = new CustomAttributeTypedArgument[count];
+ CustomAttributeTypedArgument[] cads = count != 0 ? new CustomAttributeTypedArgument[count] : Array.Empty<CustomAttributeTypedArgument>();
for (int i = 0; i < count; i++)
{
cads[i] = cats[i].CloneForApiReturn();
}
- return new CustomAttributeTypedArgument(type, cads.ToReadOnlyCollection());
+ return new CustomAttributeTypedArgument(type, Array.AsReadOnly(cads));
}
/// <summary>
using System.Diagnostics;
using System.Collections.Generic;
+using System.Collections.ObjectModel;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;
public static IList<CustomAttributeTypedArgument> ToApiForm(this IList<CustomAttributeTypedArgument<RoType>> catgs)
{
int count = catgs.Count;
- CustomAttributeTypedArgument[] cats = new CustomAttributeTypedArgument[count];
+ CustomAttributeTypedArgument[] cats = count != 0 ? new CustomAttributeTypedArgument[count] : Array.Empty<CustomAttributeTypedArgument>();
for (int i = 0; i < count; i++)
{
cats[i] = catgs[i].ToApiForm();
}
- return cats.ToReadOnlyCollection();
+ return Array.AsReadOnly(cats);
}
/// <summary>
public static IList<CustomAttributeNamedArgument> ToApiForm(this IList<CustomAttributeNamedArgument<RoType>> cangs, Type attributeType)
{
int count = cangs.Count;
- CustomAttributeNamedArgument[] cans = new CustomAttributeNamedArgument[count];
+ CustomAttributeNamedArgument[] cans = count != 0 ? new CustomAttributeNamedArgument[count] : Array.Empty<CustomAttributeNamedArgument>();
for (int i = 0; i < count; i++)
{
cans[i] = cangs[i].ToApiForm(attributeType);
}
- return cans.ToReadOnlyCollection();
+ return Array.AsReadOnly(cans);
}
/// <summary>
public static ReadOnlyCollection<T> ToReadOnlyCollection<T>(this IEnumerable<T> enumeration)
{
- // todo: use IEnumerable<T> extension: return new ReadOnlyCollection<T>(enumeration.ToArray());
List<T> list = new List<T>(enumeration);
- return new ReadOnlyCollection<T>(list.ToArray());
+ return Array.AsReadOnly(list.ToArray());
}
public static int GetTokenRowNumber(this int token) => token & 0x00ffffff;
using System.Diagnostics;
using System.Collections.Generic;
+using System.Collections.ObjectModel;
using System.Collections.Immutable;
using System.Reflection.Metadata;
ImmutableArray<RoType> sig = sigHandle.GetStandaloneSignature(reader).DecodeLocalSignature(typeProvider, TypeContext);
int count = sig.Length;
- LocalVariableInfo[] lvis = new LocalVariableInfo[count];
+ LocalVariableInfo[] lvis = count != 0 ? new LocalVariableInfo[count] : Array.Empty<LocalVariableInfo>();
for (int i = 0; i < count; i++)
{
bool isPinned = false;
lvis[i] = new RoLocalVariableInfo(localIndex: i, isPinned: isPinned, localType: localType);
}
- return lvis.ToReadOnlyCollection();
+ return Array.AsReadOnly(lvis);
}
}
{
ImmutableArray<ExceptionRegion> regions = Block.ExceptionRegions;
int count = regions.Length;
- ExceptionHandlingClause[] clauses = new ExceptionHandlingClause[count];
+ ExceptionHandlingClause[] clauses = count != 0 ? new ExceptionHandlingClause[count] : Array.Empty<ExceptionHandlingClause>();
for (int i = 0; i < count; i++)
{
EntityHandle catchTypeHandle = regions[i].CatchType;
handlerLength: regions[i].HandlerLength
);
}
- return clauses.ToReadOnlyCollection();
+ return new ReadOnlyCollection<ExceptionHandlingClause>(clauses);
}
}
{
public ReadOnlyCollection(System.Collections.Generic.IList<T> list) { }
public int Count { get { throw null; } }
+ public static System.Collections.ObjectModel.ReadOnlyCollection<T> Empty { get { throw null; } }
public T this[int index] { get { throw null; } }
protected System.Collections.Generic.IList<T> Items { get { throw null; } }
bool System.Collections.Generic.ICollection<T>.IsReadOnly { get { throw null; } }
public ReadOnlyDictionary(System.Collections.Generic.IDictionary<TKey, TValue> dictionary) { }
public int Count { get { throw null; } }
protected System.Collections.Generic.IDictionary<TKey, TValue> Dictionary { get { throw null; } }
+ public static System.Collections.ObjectModel.ReadOnlyDictionary<TKey, TValue> Empty { get { throw null; } }
public TValue this[TKey key] { get { throw null; } }
public System.Collections.ObjectModel.ReadOnlyDictionary<TKey, TValue>.KeyCollection Keys { get { throw null; } }
bool System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>.IsReadOnly { get { throw null; } }
/// </summary>
public class ReadOnlyCollectionTests : CollectionTestBase
{
- private static readonly ReadOnlyCollection<int> s_empty = new ReadOnlyCollection<int>(new int[0]);
-
[Fact]
public static void Ctor_NullList_ThrowsArgumentNullException()
{
{
var collection = new ReadOnlyCollection<int>(s_intArray);
Assert.Equal(s_intArray.Length, collection.Count);
- Assert.Equal(0, s_empty.Count);
+ }
+
+ [Fact]
+ public static void Empty_Idempotent()
+ {
+ Assert.NotNull(ReadOnlyCollection<int>.Empty);
+ Assert.Equal(0, ReadOnlyCollection<int>.Empty.Count);
+ Assert.Same(ReadOnlyCollection<int>.Empty, ReadOnlyCollection<int>.Empty);
}
[Fact]
var collection = new Collection<int>(s_intArray);
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => collection[-1]);
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => collection[s_intArray.Length]);
- AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => s_empty[0]);
+ AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => ReadOnlyCollection<int>.Empty[0]);
}
[Fact]
var collection = new ReadOnlyCollection<int>(s_intArray);
Assert.True(((IList)collection).IsReadOnly);
Assert.True(((IList<int>)collection).IsReadOnly);
- Assert.True(((IList)s_empty).IsReadOnly);
- Assert.True(((IList<int>)s_empty).IsReadOnly);
+ Assert.True(((IList)ReadOnlyCollection<int>.Empty).IsReadOnly);
+ Assert.True(((IList<int>)ReadOnlyCollection<int>.Empty).IsReadOnly);
}
[Fact]