What's the best way of creating a readonly array in C#?

C#ArraysReadonly

C# Problem Overview


I've got the extremely unlikely and original situation of wanting to return a readonly array from my property. So far I'm only aware of one way of doing it - through the System.Collections.ObjectModel.ReadOnlyCollection<T>. But that seems somehow awkward to me, not to mention that this class loses the ability to access array elements by their index (added: whoops, I missed the indexer). Is there no better way? Something that could make the array itself immutable?

C# Solutions


Solution 1 - C#

Use ReadOnlyCollection<T>. It is read-only and, contrary to what you believe, it has an indexer.

Arrays are not immutable and there is no way of making them so without using a wrapper like ReadOnlyCollection<T>.

Note that creating a ReadOnlyCollection<T> wrapper is an O(1) operation, and does not incur any performance cost.

Update
Other answers have suggested just casting collections to the newer IReadOnlyList<T>, which extends IReadOnlyCollection<T> to add an indexer. Unfortunately, this doesn't actually give you control over the mutability of the collection since it could be cast back to the original collection type and mutated.

Instead, you should still use the ReadOnlyCollection<T> (the List<T> method AsReadOnly(), or Arrays static method AsReadOnly() helps to wrap lists and arrays accordingly) to create an immutable access to the collection and then expose that, either directly or as any one of the interfaces it supports, including IReadOnlyList<T>.

Solution 2 - C#

.NET Framework 4.5 introduced IReadOnlyList<T> which extends from IReadOnlyCollection<T> adding T this[int index] { /*..*/ get; }.

You can cast from T[] to IReadOnlyList<T>. One advantage of this is that (IReadOnlyList<T>)array understandably equals array; no boxing is involved.

Of course, as a wrapper is not being used, (T[])GetReadOnlyList() would be mutable.

Solution 3 - C#

From .NET Framework 2.0 and up there is Array.AsReadOnly which automatically creates a ReadOnlyCollection wrapper for you.

Solution 4 - C#

If you really want an array returned, but are afraid that the consumer of the array will mess with the internal data, just return a copy of the Array. Personally I still think ReadOnlyCollection<T> is the way to go, but if you REALLY want an array.....

Solution 5 - C#

I recognise that this is an old question, but there are packages for .NET now that contain an ImmutableArray type. Built in to .NET Core 3, and available via NuGet for Full framework 4.5 onwards

Solution 6 - C#

IEnumerable comes to mind.

Solution 7 - C#

There is now support for Immutable collections. See https://www.nuget.org/packages/System.Collections.Immutable

This supports any of the following:

  • .NET 4.5 (or higher)
  • .NETStandard 1.0 (or higher)
  • Windows 8.0
  • WindowsPhone 8.0
  • WindowsPhoneApp 8.1
  • Portable Class Library (.NETFramework 4.5, Windows 8.0, WindowsPhone 8.0, WindowsPhoneApp 8.1)

Solution 8 - C#

You might want to implement the IEnumerable interface and overload the this[int] operator to deny access to it's setter

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVilx-View Question on Stackoverflow
Solution 1 - C#Jeff YatesView Answer on Stackoverflow
Solution 2 - C#WartyView Answer on Stackoverflow
Solution 3 - C#N8allanView Answer on Stackoverflow
Solution 4 - C#BFreeView Answer on Stackoverflow
Solution 5 - C#Matthew SteeplesView Answer on Stackoverflow
Solution 6 - C#epitkaView Answer on Stackoverflow
Solution 7 - C#HenryView Answer on Stackoverflow
Solution 8 - C#MorfildurView Answer on Stackoverflow