1024programmer Asp.Net Collection expressions (syntactic sugar for collection expressions) in C#12

Collection expressions (syntactic sugar for collection expressions) in C#12

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.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/collection-expressions-syntactic-sugar-for-collection-expressions-in-c12/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索