Collection expressions (syntactic sugar for collection expressions) in C#12
C#12 introduces new syntactic sugar for creating common collections. And you can use ..
to destructure a collection and inline it into another collection.
Supported types
- Array type, such as int[].
- System.Span and System.ReadOnlySpan.
- Supports common generic collections, such as System.Collections.Generic.List.
Using set expressions
The following shows how to use set expressions
static void Main(string[] args)
{
List names1 = ["one", "two"];
List names2 = ["three", "four"];
List<List> names3 = [["one", "two"], ["three", "four"]];
List<List> names4 = [names1, names2];
}
It can be seen that the method of use is very simple
Set expression deconstruction
In C#12, you can use ..
to deconstruct a collection and use it as an element of another collection.
static void Main(string[] args)
{
List names1 = ["one", "two"];
List names2 = ["three", "four"];
List name = [.. names1, .. names2];
}
Custom types support set expressions
Types are supported by writing the Create() method and applying the System.Runtime.CompilerServices.CollectionBuilderAttribute
to opt-in collection expressions on the collection type. The following is an example
[CollectionBuilder(typeof(LineBufferBuilder), "Create")]
public class LineBuffer : IEnumerable
{
private readonly char[] _buffer = new char[80];
public LineBuffer(ReadOnlySpan buffer)
{
int number = (_buffer.Length < buffer.Length) ? _buffer.Length : buffer.Length;
for (int i = 0; i < number; i++)
{
_buffer[i] = buffer[i];
}
}
public IEnumerator GetEnumerator() => _buffer.AsEnumerable().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _buffer.GetEnumerator();
}
internal static class LineBufferBuilder
{
internal static LineBuffer Create(ReadOnlySpan values) => new LineBuffer(values);
}
internal class Program
{
static void Main(string[] args)
{
LineBuffer line = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' ];
}
}
First, you need to create a class containing the Create method: LineBufferBuilder. The LineBufferBuilder.Create
method must return a LineBuffer object and must take a single parameter of type ReadOnlySpan.
Finally, the CollectionBuilderAttribute
must be added to the LineBuffer class declaration. The first parameter provides the name of the generator class, and the second attribute provides the name of the generator method.
Such a custom class can support set expressions.