ReadOnlySet<T> Class
public abstract class ReadOnlySet<T> : IReadOnlyCollection<T>, ISet<T>
Remarks
The ReadOnlySet<T>
class provides a read-only view of a set of elements. It implements the IReadOnlyCollection<T>
and ISet<T>
interfaces, allowing you to iterate over the elements and perform set operations without modifying the underlying collection.
This class is useful when you need to expose a set to consumers but want to ensure that it is not modified. Common scenarios include returning a set from a method that should not be changed by the caller, or creating a snapshot of a mutable set.
Summary of Members
Methods
Public Methods
Add(T item)
Always throws NotSupportedException
because writing is not supported.
Clear()
Always throws NotSupportedException
because writing is not supported.
ExceptWith(IEnumerable<T> other)
Always throws NotSupportedException
because writing is not supported.
GetEnumerator()
Returns an enumerator that iterates through the read-only set.
IntersectWith(IEnumerable<T> other)
Always throws NotSupportedException
because writing is not supported.
IsProperSubsetOf(IEnumerable<T> other)
Determines whether the current set is a proper, and thereby weak, subset of the specified collection.
IsProperSupersetOf(IEnumerable<T> other)
Determines whether the current set is a proper, and thereby weak, superset of the specified collection.
IsSubsetOf(IEnumerable<T> other)
Determines whether the current set is a subset of the specified collection.
IsSupersetOf(IEnumerable<T> other)
Determines whether the current set is a superset of the specified collection.
Overlaps(IEnumerable<T> other)
Determines whether the current set overlaps with the specified collection.
Remove(T item)
Always throws NotSupportedException
because writing is not supported.
SetEquals(IEnumerable<T> other)
Determines whether the current set and the specified collection contain the same elements.
SymmetricExceptWith(IEnumerable<T> other)
Always throws NotSupportedException
because writing is not supported.
UnionWith(IEnumerable<T> other)
Always throws NotSupportedException
because writing is not supported.
Properties
Public Properties
Count
Gets the number of elements in the set.
IsReadOnly
Gets a value that indicates whether the set is read-only.
Constructors
Protected Constructors
ReadOnlySet()
Initializes a new instance of the ReadOnlySet<T>
class.
ReadOnlySet(ISet<T> set)
Initializes a new instance of the ReadOnlySet<T>
class with the specified set.
Example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Example
{
public static void Main(string[] args)
{
var mutableSet = new HashSet { 1, 2, 3, 4, 5 };
IReadOnlySet readOnlySet = new ReadOnlyCollection(new List<int>(mutableSet)); // Example of creating a read-only view
Console.WriteLine($"Number of elements: {readOnlySet.Count}");
Console.WriteLine("Elements:");
foreach (int item in readOnlySet)
{
Console.Write($"{item} ");
}
Console.WriteLine();
// Trying to modify will throw an exception
try
{
// readOnlySet.Add(6); // This would not compile if it was IReadOnlySet directly
// If using a concrete ReadOnlySet implementation that doesn't explicitly seal Add, it would throw NotSupportedException
// For ReadOnlyCollection, the underlying collection is the source of truth for mutability.
// A true ReadOnlySet would enforce immutability directly.
}
catch (NotSupportedException)
{
Console.WriteLine("Attempted to modify the read-only set, which is not supported.");
}
}
}