C#6.0和7.0的部分新特性介绍

本文介绍了C#语言从1.0到8.0的发展历程,涵盖了各版本的发布日期和.NET Framework要求。同时,展示了C#6.0至7.0引入的重要特性,如null类型判断、字符串格式简化、元组、模式匹配、局部函数、增强版switch等,以及数字分隔符的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

语言版本发布时间.NET Framework要求
C# 1.02002.1.NET Framework 1.0
C# 1.1\1.2 2003.4.NET Framework 1.1
C# 2.0 2005.11.NET Framework 2.0 
C# 3.02007.11.NET Framework 2.0\3.0\3.5
C# 4.02010.4.NET Framework 4.0
C# 5.02012.8.NET Framework 4.5
C# 6.02015.7.NET Framework 4.6
C# 7.02017.3.NET Framework 4.6.2
C# 7.12017.6.NET Framework 4.7
C# 7.22017.11.NET Framework 4.7.1
C# 7.32018.5.NET Framework 4.7.2
C# 8.02019.4.NET Framework 4.8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using static System.Math;   //使用静态类,代码中可以直接调用方法  6.0
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            User user2 = null;
            string uName = user2?.FirstName;  //null类型判断,为null返回null  6.0
            int? age = user2?.Age;

            string myName = $"FirstName is {Name}";  //对字符串格式化简单化  6.0

            string uOfName = nameof(user2);   //获取字段名称 6.0

            Dictionary<int, string> dict1 = new Dictionary<int, string>() { [2] = "re", [43] = "rt" };   //字典初始化 6.0
            Dictionary<int, string> dict2 = new Dictionary<int, string>() { { 2, "re" }, { 43, "re" } };  //4.0Ago

            get(out int a, out int b);   //针对out可以合并 6.0
            Console.WriteLine(a);

            object obj = "gg";
            if (obj is int iObj)   //模式匹配  7.0
            {
                Console.WriteLine(iObj + 9);
            }

            var tuple = (a: 10, b: "123");   //元组(Tuples)
            Console.WriteLine($"a:{tuple.a},b:{tuple.b}");

            //解构元组 7.0
            var result1 = Get1();
            Console.WriteLine($"Item1:{result1.Item1},Item2:{result1.Item2},Item3:{result1.Item3}");
            var (str1, int1, dt1) = Get1();

            var result2 = Get2();
            Console.WriteLine($"Item1:{result2.a},Item2:{result2.b},Item3:{result2.c}");
            var (str2, int2, dt2) = Get2();

            int num = 123_456;   //允许在数字文字中_作为数字分隔符出现  7.0

            Console.ReadKey();
        }

        //解构元组
        static (string, int, DateTime) Get1()  //7.0
        {
            return ("abc", 123, DateTime.Now);
        }

        static (string a, int b, DateTime c) Get2()  //7.0
        {
            return (a: "abc", b: 123, c: DateTime.Now);
        }

        public static string Get(object a)
        {
            return GetP();
            string GetP()   //局部函数,方法中的方法  7.0
            {
                if (a is int v) return v + "";
                if (a is string b) return b;
                return "ccc";
            }
        }

        public static void PrintStars(object o)
        {
            switch (o)   //加强版switch  7.0
            {
                case Print p:
                    break;
                case int a:
                    break;
                case String b when b == "123":
                    break;
            }
        }

        public class Print
        {
            public string PrintName { get; set; }
            public string MoBanPath { get; set; }
            public int Count { get; set; }
        }


        public static int get(out int a, out int b)
        {
            a = 8; b = 9;
            return a + b;
        }

        public static string Name { get; set; } = "BB";    //直接对get属性赋初始值  6.0

        public static int Age { get; } = 100;

        public class User
        {
            public string FirstName { get; set; }

            public string LastName { get; set; }

            public override string ToString() => string.Format("{0}——{1}", FirstName, LastName);   //Lambda简写方法  6.0

            public void S() => string.Format("{0}——{1}", FirstName, LastName);

            public string FullName => FirstName + " " + LastName;

            public int Age { get; set; } = 35;

            public int ID
            {
                get => 0;
                set => Age = value;
            }
        }


    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bridge_go

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值