CollectionExtensions.AsReadOnly Method

Definition

Overloads

AsReadOnly<T>(IList<T>)

Returns a read-only ReadOnlyCollection<T> wrapper for the specified list.

AsReadOnly<TKey,TValue>(IDictionary<TKey,TValue>)

Returns a read-only ReadOnlyDictionary<TKey,TValue> wrapper for the current dictionary.

AsReadOnly<T>(IList<T>)

Source:
CollectionExtensions.cs
Source:
CollectionExtensions.cs
Source:
CollectionExtensions.cs

Returns a read-only ReadOnlyCollection<T> wrapper for the specified list.

C#
public static System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly<T>(this System.Collections.Generic.IList<T> list);

Type Parameters

T

The type of elements in the collection.

Parameters

list
IList<T>

The list to wrap.

Returns

An object that acts as a read-only wrapper around the current IList<T>.

Exceptions

list is null.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

AsReadOnly<TKey,TValue>(IDictionary<TKey,TValue>)

Source:
CollectionExtensions.cs
Source:
CollectionExtensions.cs
Source:
CollectionExtensions.cs

Returns a read-only ReadOnlyDictionary<TKey,TValue> wrapper for the current dictionary.

C#
public static System.Collections.ObjectModel.ReadOnlyDictionary<TKey,TValue> AsReadOnly<TKey,TValue>(this System.Collections.Generic.IDictionary<TKey,TValue> dictionary);

Type Parameters

TKey

The type of keys in the dictionary.

TValue

The type of values in the dictionary.

Parameters

dictionary
IDictionary<TKey,TValue>

The dictionary to wrap.

Returns

An object that acts as a read-only wrapper around the current IDictionary<TKey,TValue>.

Exceptions

dictionary is null.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9