zoukankan      html  css  js  c++  java
  • C#7 的一些新语法

    1. Out参数

                #region 1.Out参数
                var str = "2";
                ///之前的写法
                {
                    int iValue;
                    //将str转化成int类型,若转化成功,将值赋给iValue,并返回true;若转化失败,返回false。
                    if (int.TryParse(str, out iValue))
                    {
                        WriteLine(iValue);
                    }
                }
                //C#7的写法
                {
                    if (int.TryParse(str, out int iValue))
                    {
                        WriteLine(iValue);
                    }
                    //或者
                    if (int.TryParse(str, out var vValue))
                    {
                        WriteLine(iValue);
                    }
                }
                #endregion

    2. 模式

                #region 2.模式 pattern
                {
                    WriteLine("C#7_2.模式 pattern:");
                    this.PrintStars(null);
                    this.PrintStars(2);
                }
            /// <summary>
            ///  具有模式的 IS 表达式
            /// </summary>
            private void PrintStars(Object oValue)
            {
                if (oValue is null) return;// 常量模式 "null"
                if (!(oValue is int iValue)) return;// 类型模式 定义了一个变量 "int i"
                WriteLine(iValue);
                WriteLine(new string('*', iValue));
            }

    3. Swith

                #region 3. Swith
                {
                    WriteLine("C#7_3. Swith新语法:");
                    this.Swith("wang");
                    this.Swith("Max");
                    this.Swith("wangMax");
                    this.Swith(null);
                    this.Swith("");
                }
                #endregion
            /// <summary>
            /// 可以设定任何类型的 Switch 语句(不只是原始类型)
            /// 模式可以用在 case 语句中
            /// Case 语句可以有特殊的条件
            /// </summary>
            private void Swith(string sValue)
            {
                int iValue = 1;
                switch (sValue)
                {
                    case string s when s.Length >= 4:
                        WriteLine("s");
                        break;
                    case "Max" when sValue.Length > 2:
                        WriteLine(sValue);
                        break;
                    default:
                        WriteLine("default");
                        break;
                    case null:
                        WriteLine("null");
                        break;
                }
            }

    4. 数字分隔符【每隔3位+"_"】

                #region 4.数字分隔符【每隔3位+"_"】
                long lValue = 100_000_000;
                WriteLine(lValue);
                #endregion

    5. 局部函数

                #region 5. 局部函数
                {
                    WriteLine("C#7_6. 局部函数:");
                    Add(3);
                    int Add(int i)
                    {
                        WriteLine(2 + i);
                        return i;
                    }
                }
                #endregion

    6. 元组

                #region 6.元组
                {
                    var result = this.LookupName(1);
                    WriteLine(result.Item1);
                    WriteLine(result.Item2);
                    WriteLine(result.Item3);
                  
                }
                {
                    var result = this.LookupNameByName(1);
                    WriteLine(result.first);
                    WriteLine(result.middle);
                    WriteLine(result.last);
    
                    WriteLine(result.Item1);
                    WriteLine(result.Item2);
                    WriteLine(result.Item3);
                }
                #endregion
            /// <summary>
            /// System.ValueTuple 需要安装这个nuget包
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            private (string, string, string) LookupName(long id) // tuple return type
            {
                return ("first", "middle", "last");
            }
    
            private (string first, string middle, string last) LookupNameByName(long id) // tuple return type
            {
                return ("first", "middle", "last");
                //return (first: "first", middle: "middle", last: "last");
            }
  • 相关阅读:
    洛谷 P1194 飞扬的小鸟 题解
    洛谷 P1197 星球大战 题解
    洛谷 P1879 玉米田Corn Fields 题解
    洛谷 P2796 Facer的程序 题解
    洛谷 P2398 GCD SUM 题解
    洛谷 P2051 中国象棋 题解
    洛谷 P1472 奶牛家谱 Cow Pedigrees 题解
    洛谷 P1004 方格取数 题解
    洛谷 P2331 最大子矩阵 题解
    洛谷 P1073 最优贸易 题解
  • 原文地址:https://www.cnblogs.com/wangwangwangMax/p/14070313.html
Copyright © 2011-2022 走看看