zoukankan      html  css  js  c++  java
  • C# 可选参数 命名参数

    1.可选参数

            可选参数是.NET4中新添加的功能,应用可选参数的方法在被调用的时可以选择性的添加需要的参数,而不需要的参数由参数默认值取代。

    class Program
        {
            /// <summary>
            /// 可选参数  命名参数
            /// </summary>
            static void Main(string[] args)
            {
                Console.WriteLine(ShowComputer());
                Console.WriteLine(ShowComputer("P5300","1G"));
                Console.Read();
            }
     
            private static string ShowComputer(string cpu = "i3 370M", string ram = "4G", string disk = "320G")
            {
                return "My computer ... \nCpu:" + cpu + "\nRam:" + ram + "\nDisk:" + disk + "\n";
            }
        }

      代码运行的结果图下图:

    2.命名参数

              命名参数是把参数附上参数名称,这样在调用方法的时候不必按照原来的参数顺序填写参数,只需要对应好参数的名称也能完成方法。

    class Program
        {
            /// <summary>
            /// 可选参数  命名参数
            /// </summary>
            static void Main(string[] args)
            {
                Console.WriteLine(ShowComputer("i3 370M","2G","320G"));
                Console.WriteLine(ShowComputer(disk: "320G", cpu: "i3 370M", ram: "2G"));
                Console.Read();
            }
     
            private static string ShowComputer(string cpu, string ram, string disk)
            {
                return "My computer ... \nCpu:" + cpu + "\nRam:" + ram + "\nDisk:" + disk + "\n";
            }
        }

      以上代码两次输出的结果应该是相同的,运行结果如下图:

         命名参数如果只是改变参数的顺序,这样的意义并不大,我们没有必要为了改变顺序而去用命名参数,他与可选参数结合才能显示出他真正的意义。

    class Program
        {
            /// <summary>
            /// 可选参数  命名参数
            /// </summary>
            static void Main(string[] args)
            {
                Console.WriteLine(ShowComputer(ram: "3G"));
                Console.Read();
            }
     
            private static string ShowComputer(string cpu = "i3 370M", string ram = "2G", string disk = "320G")
            {
                return "My computer ... \nCpu:" + cpu + "\nRam:" + ram + "\nDisk:" + disk + "\n";
            }
        }

      程序只赋值了第二个参数ram,其他参数均为默认值,运行结果大家应该都知道了。这样命名参数和可选参数都发挥了他们独特的作用。

  • 相关阅读:
    HDU 3681 Prison Break(状态压缩dp + BFS)
    POJ 2711 Regular Words(DP + 高精度)
    ZOJ 2745 01-K Code(DP)(转)
    DAG模型——硬币问题
    HDU 1619 Unidirectional TSP(单向TSP + 路径打印)
    DAG模型——嵌套矩阵
    HDU 4294 A Famous Equation(DP)
    HDU 3920 Clear All of Them I(DP + 状态压缩 + 贪心)
    POJ 3254 Corn Fields(DP + 状态压缩)
    HDU 2089 不要62(数位DP)
  • 原文地址:https://www.cnblogs.com/monian/p/2797434.html
Copyright © 2011-2022 走看看