zoukankan      html  css  js  c++  java
  • C#7.0新特性

    out变量

    可以直接在方法中使用out申明变量 int.TryParse("123", out var result); 

    元组

    元组的申明

    var alphaBetaStart = (alpha: "a", beta: "b");
    Console.WriteLine($"{alphaBetaStart.alpha}{alphaBetaStart.beta}");

    解构与元组

    private class User
    {
      public string Name { get; } = "Bob";
      public string Email { get; } = "129120@qq.com";
      public int Age { get; set; } = 25;
    
      //解构
      public void Deconstruct(out string name, out string email)
      {
        (name, email) = (Name, Email);
      }
    }
    
    //解构元组
    var u = new User();
    var (name, email) = u;
    Console.WriteLine($"{name}{email}");    

    弃元

    //弃元
     var (_, email2) = u;
     Console.WriteLine($"{email2}");

    使用_代替了解构的返回值name

    模式匹配

    什么是模式匹配

    官方文档的定义是:模式匹配是一种可让你对除对象类型以外的属性实现方法分派的功能;简而言之对于某一个抽象对象的抽象方法,他不同的子类有不同实现,现在我们也可以对属性使用is关键字进行类别判断进行不同的操作

     if (input is int i)
    {
        Console.WriteLine($"{i}是int类型");
    }
    else if (input is string j)
    {
        Console.WriteLine($"{j}是string类型");
    }
    
    private object input = "123";

     switch匹配模式

    上面例子是is的模式匹配,使用switch可以编写更加复杂的模式匹配

                var nus = new List<int>
                {
                    1, 2, 3, 4, 5, -1, -8, -10
                };
                var sequence = new List<object>
                {
                    0, -10, -20, 10, new User(), 20, "book", "doom", "time", nus
                };
    
                //switch的模式匹配
                foreach (var content in sequence)
                {
                    var sum = 0;
                    switch (content)
                    {
                        case 0:
                            Console.WriteLine("0");
                            break;
                        case IEnumerable<int> child:
                        {
                            foreach (var item in child) sum += item < 0 ? item : 0;
                            Console.WriteLine($"child:{sum}");
                            break;
                        }
                        case int n when n > 0:
                            Console.WriteLine($"n:{n}");
                            break;
                        case string s when s.Contains("oo"):
                            Console.WriteLine($"s:{s}");
                            break;
                        default:
                            Console.WriteLine("default");
                            break;
                    }
                }
    View Code

    Ref 局部变量和返回结果

    对于引用类型A,若将A赋值给B(B=A),改变B的值则A也会改变。而对于值类型(如int),若将D赋值给C,改变C的值D值不变。现在使用ref关键字可以完成改变C导致D的值改变

    int x = 10;
    ref var num = ref x;//使用ref将x的赋值给num,虽然是struct但是改变num也会改变x,反之亦然,相当于num和x是引用类型
    num = 9;

    此时输出x,则其值为9

    也可在方法中使用

            public ref int RefVariable(int[,] matrix)
            {       
                for (var i = 0; i < 3; i++)
                {
                    for (var j = 0; j < 3; j++)
                    {
                        if (matrix[i, j] % 2 == 0)
                        {
                            return ref matrix[i, j];
                        }
                    }
                }
                throw new InvalidOperationException("Not found");
            }

    此时如果改变返回值,matrix表示的二维数组该对象值也会改变

    本地函数(局部函数)

            public IEnumerable<int> LocalFunction(int min,int max)
            {
                if(min < 0 || min > 10)
                    throw new ArgumentException("min范围是0到10");
                if (max < 0 || max > 10)
                    throw new ArgumentException("max范围是0到10");
                if(min > max)
                    throw new ArgumentException("max必须不小于min");
                return Get24();
    
                IEnumerable<int> Get24()
                {
                    for (int i = min; i < max; i++)
                    {
                        if (i == 2 || i == 4)
                            yield return i;
                    }
                }
            }

    属性表达式

            private string _name;
    
            public string Name
            {
                get => _name;
                set => _name = value ?? string.Empty;
            }

    通用的异步返回类型

            /// <summary>
            /// 使用ValueTask代替引用类型的Task
            /// </summary>
            /// <returns></returns>
            private async ValueTask<int> Func()
            {
                await Task.Delay(10);
                return 5;
            } 

    数字文本语法改进与默认文本表达式

    数字文本语法改进

            /// <summary>
            /// 数字文本语法改进0b开头表示二进制_分割方便阅读,不影响数值
            /// </summary>
            public void NumberText()
            {
                int a = 0b0001_0001;
                int b = 100_100;
            }

    默认文本表达式

            /// <summary>
            /// 默认文本表达式
            /// </summary>
            public Func<string, bool> Func1 { get; set; } = default;
  • 相关阅读:
    Notes of Daily Scrum Meeting(12.18)
    Notes of Daily Scrum Meeting(12.17)
    Notes of Daily Scrum Meeting(12.16)
    Notes of Daily Scrum Meeting(12.8)
    Notes of Daily Scrum Meeting(12.5)
    Notes of Daily Scrum Meeting(12.3)
    Notes of Daily Scrum Meeting(11.12)
    Linux中profile、bashrc、bash_profile之间的区别和联系
    Linux GCC编译
    mysql 5.7.16 远程连接
  • 原文地址:https://www.cnblogs.com/doomclouds/p/13239313.html
Copyright © 2011-2022 走看看