Support for 'Collection Expressions'?

I’ve been learning some of the new features in C#12 and .Net 8.0 and came across 'collection expressions’. I tried to run a few code samples in the latest versions of Fire and Water (target framework set to .Net 8.0) but with no luck. I’m able to run the same code in Rider IDE, Visual Studio 2022 and from the command line (eg. dotnet run) :cowboy_hat_face:

‘IDE0028: Use collection initializers or expressions’ documentation

‘IDE0300: Use Collection Expression for Array’ documentation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CollectionExpression_CS1
{
    static class Program
    {
        public static Int32 Main(string[] args)
        {
            //===================== Tradtional way pre C#12 ========================//
            Console.WriteLine("The magic happens here.");
            // Array:
            int[] myArray1 = new int[] { 10, 20, 30, 40, 50 };
            var myArray2 = new int[] { 10, 20, 30, 40, 50 };

            // List:
            List<string> myList1 = new List<string> { "apple", "banana", "orange" };
            var myList2 = new List<string> { "apple", "banana", "orange" };

            // Span:
            var array3 = new char[] { 'a', 'b', 'c', 'd', 'e' };
            Span<char> mySpan1 = array3.AsSpan();
            var mySpan2 = array3.AsSpan();

            //===================== with Collection Expressions =====================//
            // Array
            int[] demoArray = [10, 20, 30, 40, 50];

            // List
            List<string> demoList = ["apple", "banana", "orange"];

            // Span
            Span<char> demoSpan = ['a', 'b', 'c', 'd', 'e'];
            
            //=======================================================================//
            
            int[] array1 = { 1, 2, 3 };
            int[] array2 = [4, 5, 6];  // using collection expression 
            
            // Traditional way
            int[] combined = new int[array1.Length + array2.Length];
            Array.Copy(array1, 0, combined, 0, array1.Length);
            Array.Copy(array2, 0, combined, array1.Length, array2.Length);
            
            // Using collection expression initialization
            int[] combined1 = [..array1, ..array2];
            
            foreach( var elem in combined1) 
            Write(elem); // 123456
            WriteLine();
            return 0;
        }
    }
}

Hi Gary,

that is expected, we have n ot shipped support for C# 12 features yet, but we are investigating.

thanx,
marc

1 Like

Not a problem Marc. It’s interesting when this Oxgyene statement:

var Items2 := new List<String>(["One", "Two", "Three"]);

is directly converted to C#:

var Items2 = new List<String>(["One", "Two", "Three"]);

Rider IDE (.Net 7.0) sees it as a Collection Expression.