C#新语法

C#6语法
1.只读自动属性
2.using static
3.Null 条件运算符
3.字符串内插
4.异常筛选器
5.nameof 表达式
6.使用索引器初始化关联集合
 
 
C#7语法
1.out 变量
原:先声明,后使用
现:不需要声明
int.TryParse("456",out var result);
Console.WriteLine(result);

2.元组
   class Program
    {
        static void Main(string[] args)
        {
            //int.TryParse("456", out var result);
            //Console.WriteLine(result);

            //元组
            (string Alpha, string Beta) namedLetters = ( "a", "b" );
            namedLetters.Alpha = "aa";
            namedLetters.Beta = "bb";
            Console.WriteLine($"{namedLetters.Alpha},{namedLetters.Beta}");

            var alphabetStart = (Alpha: "a", Beta: "b");
            alphabetStart.Beta = "B+B";
            alphabetStart.Alpha = "A+A";
            Console.WriteLine($"{alphabetStart.Alpha},{alphabetStart.Beta}");

            (int max, int min) = Range();
            Console.WriteLine(max);
            Console.WriteLine(min);

            {
                var p = new Point(12, 13);
                Console.WriteLine(p.X);
                Console.WriteLine(p.Y);
                p.Deconstruct(out double xx, out double yy);
                Console.WriteLine(xx);
                Console.WriteLine(yy);
            }

        }
        private static (int max,int min)Range()
        {
            return (123, 234);
        }
    }
    public class Point 
    {
        public Point(double x, double y) => (X, Y) = (x, y);

        public double X { get; }
        public double Y { get; }

        public void Deconstruct(out double x, out double y) => (x, y) = (X, Y);
    }

 

3.弃元
用不到的参数不进行返回值
4.模式匹配
    class Program
    {
        static void Main(string[] args)
        {
            #region 模式
            {
                int input = 123;
                int sum = 234;
                if(input is int count)//input 是int类型变量,则将input值赋值给count
                {
                    sum += count;
                }
            }
            {
                IEnumerable<object> enumerablelist = new List<object>()
                {
                    0,
                    new List<int>(){0,1,2,3,4,5,6},
                    100,
                    null
                };
                int iResult = SumPositiveNumbers(enumerablelist);
            }
            #endregion

        }
        public static int SumPositiveNumbers(IEnumerable<object> sequence)
        {
            int sum = 0;
            foreach(var i in sequence)
            {
                switch(i)
                {
                    case 0:
                        break;
                    case IEnumerable<int> childSequence:
                        {
                            foreach (var item in childSequence)
                                sum += (item > 0) ? item : 0;
                            break;
                        }
                    case int n when n > 0:
                        sum += n;
                        break;
                    case null:
                        throw new NullReferenceException("NUll found in sequence");
                    default:
                        throw new InvalidOperationException("Unrecognizes type");
                }
            }
            return sum;
        }

 

5.ref 局部变量和返回结果
6.本地函数
        static void Main(string[] args)
        {
            #region 本地方法
            LocalFunction("朝夕教育");
            #endregion
        }

        public static string LocalFunction(string name)
        {
            return ZhxiToString(name);
            string ZhxiToString(string name)
            {
                return name;
            }
        }

7.更多的 expression-bodied 成员

8.throw 表达式

9.通用的异步返回类型

10.数字文本语法改进

C#8语法
1.Readonly 成员 待处理
2.默认接口方法
1namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 默认接口方法
            SharpEightInterface sharpInterface = new ShiXian();
            sharpInterface.show();
            sharpInterface.ShowInfo();
            #endregion
        }
    }
}
2、接口
namespace ConsoleApp1
{
    interface SharpEightInterface
    {
        public void show();
        public void ShowInfo()//interface中也可以写方法
        {
            Console.WriteLine("this is ShowInfo");
        }
    }
}
3、实现
namespace ConsoleApp1
{
    class ShiXian: SharpEightInterface
    {
        public void show()
        {
            Console.WriteLine("this is Show");
        }
    }
}
3.模式匹配增强功能:
4.Using 声明
5.静态本地函数
6.可处置的 ref 结构
7.可为空引用类型
8.异步流
9.异步可释放
10.索引和范围
11.Null 合并赋值
12.非托管构造类型
13.嵌套表达式中的
 
14.Stackalloc
15.内插逐字字符串的增强功能
16.Switch
    class Program
    {
        public enum WeekInfo
        {
            Monday,
            Tuesday
        }
        static void Main(string[] args)
        {

            #region Switch
            //老玩法
            string week = WeekToStringSwitch(WeekInfo.Monday);
            //新玩法
            string weeknew = WeekToString(WeekInfo.Tuesday);
            #endregion
        }
        public static string WeekToStringSwitch(WeekInfo week)
        {
            switch(week)
            {
                case WeekInfo.Monday:
                    return "周一";
                case WeekInfo.Tuesday:
                    return "周二";
                default:
                    throw new NotImplementedException("枚举不存在");
            }
        }

        public static string WeekToString(WeekInfo week) => week switch
        {
            WeekInfo.Monday => "周一",
            WeekInfo.Tuesday => "周二",
            _ => throw new NotImplementedException("枚举不存在"),
        };
}

 17.属性模式

static void Main(string[] args)
        {
            #region Switch属性模式
            PropertyPattern product = new PropertyPattern
            {
                ProductName = "朝夕架构班",
                Price = 5499
            };
            PropertyPattern product1 = new PropertyPattern
            {
                ProductName = "朝夕高级班",
                Price = 4299
            };
            double price = PropertyPatternShow(product);
            #endregion

        }
        public static double PropertyPatternShow(PropertyPattern pattern) => pattern switch
        {
            { ProductName: "朝夕架构班" } => pattern.Price * 0.5,
            _ => throw new NotImplementedException(),
        };
    public class PropertyPattern
    {
        public string ProductName { get; set; }
        public double Price { get; set; }
    }

 


C#9语法(需要二次观看视频)
1.记录
2.仅限 Init 的资源库
3.顶级语句
4.模式匹配增强功能
5.本机大小的整数
6.函数指针
7.禁止发出 localsinit 标志
 
 
8.目标类型的新表达式
9.静态匿名函数
10.目标类型的条件表达式
11.协变返回类型
12.扩展 GetEnumerator 支持 foreach 循环
13.Lambda 弃元参数
14.本地函数的属性
15.模块初始值设定项
16.分部方法的新功能
posted @ 2023-09-18 13:28  它的眼角开过光  阅读(65)  评论(0编辑  收藏  举报