• 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 ... 
    Cpu:" + cpu + "
    Ram:" + ram + "
    Disk:" + disk + "
    ";
            }
        }
    

      

    代码运行的结果图下图:

    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 ... 
    Cpu:" + cpu + "
    Ram:" + ram + "
    Disk:" + disk + "
    ";
            }
        }
    

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

    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 ... 
    Cpu:" + cpu + "
    Ram:" + ram + "
    Disk:" + disk + "
    ";
            }
        }
    

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

  • 相关阅读:
    寻找重复数
    除自身以外数组的乘积
    汇总区间
    Atlas 分表功能
    Atlas 读写分离 & Atlas + MHA 故障自动恢复
    MHA 的 Binlog Server & VIP 漂移
    MHA 高可用介绍
    MySQL 主从复制(下)
    MySQL 基础面试题
    MySQL 主从复制(上)
  • 原文地址:https://www.cnblogs.com/lgx5/p/9236937.html
Copyright © 2020-2023  润新知