[API Proposal]: Supporting collection expression []
for empty ReadOnlyCollection<T>.Empty
#110942
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Collections
Background and motivation
The
ReadOnlyCollection<T>
type in C# is a widely used wrapper for ensuring the immutability of collections from consumers. It is commonly used to expose collection data in APIs while guaranteeing that consumers cannot modify it. However, working with empty instances ofReadOnlyCollection<T>
often requires verbose syntax that diminishes readability and convenience in codebases where immutability and empty collections are frequent.Currently, to assign an empty
ReadOnlyCollection<T>
, developers must use the static propertyReadOnlyCollection<T>.Empty
or construct an empty instance manually using a backing empty array. This verbosity can lead to less concise and harder-to-read code. For example:In contrast, C# already provides a concise and elegant syntax for empty arrays using the collection expression
[]
, which is translated toArray.Empty<T>()
when appropriate. Introducing support for the same[]
syntax forReadOnlyCollection<T>.Empty
would offer the following benefits:Improved Developer Experience
The
[]
syntax simplifies code by reducing boilerplate when working with immutable empty collections. This improves readability, especially in scenarios involving complex data structures or configuration setups where immutability is key.Symmetry and Consistency Across Collection APIs
Supporting
[]
forReadOnlyCollection<T>.Empty
aligns with existing patterns in C# for empty arrays (Array.Empty<T>()
) and other concise initializations (e.g.,new List<int>()
or LINQ.ToList()
calls). This symmetry enhances the intuitiveness of the language.Encouraging Best Practices with Immutability
Many APIs recommend returning immutable empty collections instead of
null
to reduce the risk of null reference exceptions. By making immutable empty collections easier to use, this feature promotes better programming practices.Prevalence of Empty Collections in Real-World Scenarios
Use cases for immutable empty collections are common, including:
Reduction of Boilerplate Code
Consider the following example:
With the proposed feature, this can be simplified to:
This reduction in boilerplate code makes the developer’s intent clearer and the code more readable.
By enabling the collection expression
[]
to representReadOnlyCollection<T>.Empty
, the language gains a powerful tool for expressing immutability succinctly while maintaining consistency with existing patterns in C#.API Proposal
Enable the following syntax for creating an immutable empty
ReadOnlyCollection<T>
:The collection expression
[]
, when assigned to aReadOnlyCollection<T>
, will be translated to a reference to the existing singleton instance ofReadOnlyCollection<T>.Empty
. Internally, this will leverage the following implementation:This ensures that the
[]
syntax remains consistent with other uses of empty collections in C# while maintaining high performance and memory efficiency by reusing the singleton instance.API Usage
1. Variable Initialization
The
[]
expression can be used to initialize an emptyReadOnlyCollection<T>
directly, making the code concise and readable.2. Method Parameters
The
[]
syntax can be used to set default values forReadOnlyCollection<T>
parameters.3. Return Values
You can use the
[]
expression to return an immutable empty collection from methods.4. Conditional Expressions
The
[]
syntax works seamlessly in conditional expressions for initializing empty collections based on a condition.5. LINQ and Transformations
The
[]
expression simplifies scenarios where you need to return an empty immutable collection based on a LINQ query result.6. Nested Initializations
The
[]
syntax can be used in nested object initializations, such as for default properties or fields.Summary of Benefits
ReadOnlyCollection<T>.Empty
.Array.Empty<T>()
and other modern collection initialization patterns in C#.Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: