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 @   FredGrit  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2023-06-05 cpp test write content speed to ssd and usual disk respectively 1M,10M,100M rows data,the fact has illustrated the write speed of ssd is 4-5 times faster than usual disk
2023-06-05 c++ condition_variable wait unique_lock,cv.notifyall()
2023-06-05 c++ tree data structure
2020-06-05 C# deflatestream compress and decompress, compare their respective md5
2020-06-05 C# BinaryWriter BinaryReader demo
2019-06-05 C# NPOI Export DataTable C# NPOI导出DataTable 单元格自适应大小
点击右上角即可分享
微信分享提示