C# 12 new feature Collection Expression,Primary Consctructor,Generic type alias,

using listOfInt = System.Collections.Generic.List<int>;

namespace ConsoleApp36
{
    internal class Program
    {
        static void Main(string[] args)
        {
            GenericTypeAlias();
        }

        static void GenericTypeAlias()
        {
            var list = new listOfInt();
            for(int i=0;i<100;i++)
            {
                list.Add(i*i);
            }

            for(int i=0;i<100; i++)
            {
                Console.WriteLine(list[i]);
            }
        }

        static void DefaultLambdaExpressionDemo()
        {
            var defaultLambdaExpression = (string time = "") => Console.WriteLine(time);
            defaultLambdaExpression("time now");
        }

        static void CollectionExpression()
        {
            char[] vowels = ['a', 'e', 'i', 'o', 'u'];
            foreach (var ch in vowels)
            {
                Console.WriteLine(ch);
            }

            List<int> list = [10, 20, 30, 40, 50, 60, 70];
            foreach(var i in list)
            {
                Console.WriteLine(i);
            }
        } 

        static void PrimaryConstrutorDemo()
        {
            PrimaryConstructor pc = new PrimaryConstructor(1, "test");
            pc.Print();
        } 

    }

    class PrimaryConstructor(int id,string name)
    {
        public void Print() => Console.WriteLine($"Id:{id},Name:{name}");
    }

}

 

posted @ 2024-06-05 11:56  FredGrit  阅读(3)  评论(0编辑  收藏  举报