您的位置:威尼斯官方网站 > 威尼斯正规官网 > 混合型命名

混合型命名

发布时间:2020-01-06 06:55编辑:威尼斯正规官网浏览(102)

    意气风发.out的形参变量无需再提前声明

    befor:

    string input = "3";
    int numericResult;
    if (int.TryParse(input, out numericResult))
        Console.WriteLine(numericResult);
    else
        Console.WriteLine("Could not parse input");
    

    after:

    string input = "3";
    if (int.TryParse(input, out var numericResult))
        Console.WriteLine(numericResult);
    else
        Console.WriteLine("Could not parse input");
    

    二.扩展了元组(Tuple的利用,供给Nuget援引 System.ValueTuple)

    1.命名的匡正:

    ①.无命名,仅能由此无意义的Item1,Item2实行寻访:

    var letters = ("a", "b");
    Console.WriteLine($"Value is {letters.Item1} and {letters.Item2>}");
    

    befor:

    (string first, string second) letters = ("a", "b");
    Console.WriteLine($"Value is {letters.first} and {letters.second}");
    

    after:

    var letters = (first: "a",second: "b");
    Console.WriteLine($"Value is {letters.first} and {letters.second}");
    

    混合型命名:(会有二个编写翻译警报,仅以右边手命名字为准)

    (string leftFirst,string leftSecond) letters = (first: "a", second: "b");
    Console.WriteLine($"Value is {letters.leftFirst} and {letters.leftSecond}");
    

    2.函数再次来到元组、对象转元组

    static void Main(string[] args)
    {
        var p = GetData();
        Console.WriteLine($"value is {p.name} and {p.age}");
    }
    
    private static (string name,int age) GetData()
    {
        return ("han mei", 23);
    }
    

    三.Local function (本地函数卡塔尔国

    static void Main(string[] args)
            {
                var v = Fibonacci(3);
                Console.WriteLine($"value is {v}");
            }
    
            private static int Fibonacci(int x)
            {
                if (x < 0) throw new ArgumentException("Less negativity please!", nameof(x));
                return Fib(x).current;
    
                (int current, int previous) Fib(int i)
                {
                    if (i == 0) return (1, 0);
                    var (p, pp) = Fib(i - 1);
                    return (p + pp, p);
                }
            }
    

    四.Literal improments(字义修正卡塔尔

    1.数字间能够扩大分隔符:_ (扩张可读性卡塔尔国

    2.得以向来表明二进制 (使用二进制的气象更有利,譬如掩码、用位举办权力设置等)

    var d = 123_456;
    var x = 0xAB_CD_EF;
    var b = 0b1010_1011_1100_1101_1110_1111;
    

    五.Ref returns and locals (再次来到引用[再次回到的变量能够是贰个援用])

    static void Main(string[] args)
    {
        int[] array = { 1, 15, -39, 0, 7, 14, -12 };
        ref int place = ref Find(7, array); // aliases 7's place in the array
        place = 9; // replaces 7 with 9 in the array
        Console.WriteLine(array[4]); // prints 9
    }
    
    
    private static ref int Find(int number, int[] numbers)
    {
        for (int i = 0; i < numbers.Length; i++)
        {
            if (numbers[i] == number)
            {
                return ref numbers[i]; // return the storage location, not the value
            }
        }
        throw new IndexOutOfRangeException($"{nameof(number)} not found");
    }
    

    六.More expression bodied members(更加多的发挥式体的积极分子卡塔尔国

    支撑越多的积极分子利用表明式体,参预了访问器、构造函数、析构函数使用表明式体

    class Person
    {
        private static ConcurrentDictionary<int, string> names = new ConcurrentDictionary<int, string>();
        private int id = 123;
    
        public Person(string name) => names.TryAdd(id, name); // constructors
        ~Person() => names.TryRemove(id, out var v);              // destructors
        public string Name
        {
            get => names[id];                                 // getters
            set => names[id] = value;                         // setters
        }
    }
    

    七.Throw expressions(抛出表明式State of Qatar

    将十分直白当做表明式抛出,不管是用表述式体时,依然日常的return 时能够从来作为叁个表达式来写。

    class Person
    {
        public string Name { get; }
        public Person(string name) => Name = name ?? throw new ArgumentNullException(name);
        public string GetFirstName()
        {
            var parts = Name.Split(' ');
            return (parts.Length > 0) ? parts[0] : throw new InvalidOperationException("No name!");
        }
        public string GetLastName() => throw new NotImplementedException();
    }
    

    八.Generalized async return types(周全异步再次回到类型卡塔尔国

    需要Nuget引用System.Threading.Tasks.Extensions。异步时能返回更多的类型。**

     

    public async ValueTask<int> Func()
    {
        await Task.Delay(100);
        return 5;
    }
    

    九.Pattern matching(形式相称)

    1. is 发布式 ,剖断项目标还要创造变量

    public static int DiceSum2(IEnumerable<object> values)
    {
        var sum = 0;
        foreach(var item in values)
        {
            if (item is int val)
                sum += val;
            else if (item is IEnumerable<object> subList)
                sum += DiceSum2(subList);
        }
        return sum;
    }
    
    1. switch 表达式,允许case后的原则判别的还要创设变量

      public static int DiceSum5(IEnumerable values) {

       var sum = 0;
       foreach (var item in values)
       {
           switch (item)
           {
               case 0:
                   break;
               case int val:
                   sum += val;
                   break;
               case PercentileDie die:
                   sum += die.Multiplier * die.Value;
                   break;
               case IEnumerable<object> subList when subList.Any():
                   sum += DiceSum5(subList);
                   break;
               case IEnumerable<object> subList:
                   break;
               case null:
                   break;
               default:
                   throw new InvalidOperationException("unknown item type");
           }
       }
       return sum;
      

      }

      参照他事他说加以考察剧情:

      本文由威尼斯官方网站发布于威尼斯正规官网,转载请注明出处:混合型命名

      关键词: