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,其他参数均为默认值,运行结果大家应该都知道了。这样命名参数和可选参数都发挥了他们独特的作用。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值