不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
.NET Framework 4.0命名参数和可选参数
编辑:dnawo 日期:2013-09-29
1.命名参数
注意事项
·调用时命名参数要放在位置参数后面,不然会出错,例如:Test(d: 4, c: 3, 1, 2);
2.可选参数
注意事项
·声明时可选参数要放在必选参数后面,不然会出错,例如:static void Test(int a = 1, int b =2 , int c = 3, int d);
·调用时为某个可选参数传入实参,则其前面的所有参数都必须传入实参,不然会出错,例如:Test(, 6);
小结
命名参数和可选参数必须一起使用才能显示出优势。若没有可选参数,调用时所有参数必须传值,还不如不用命名参数;若没有命名参数,调用时为某个可选参数传入实参时,其前面所有参数都必须传值,可选参数变得没那么完美。
最后,再看一个例子:
参数资料
[1].命名实参和可选实参(C# 编程指南):http://msdn.microsoft.com/zh-cn/library/vstudio/dd264739.aspx
复制内容到剪贴板
程序代码

class Program
{
static void Test(int a, int b, int c, int d)
{
Console.WriteLine("{0},{1},{2},{3}", a, b, c, d);
}
static void Main(string[] args)
{
Test(d: 4, c: 3, b: 2, a: 1); //1,2,3,4
Test(5, 6, c: 7, d: 8); //5,6,7,8
Console.ReadKey();
}
}
{
static void Test(int a, int b, int c, int d)
{
Console.WriteLine("{0},{1},{2},{3}", a, b, c, d);
}
static void Main(string[] args)
{
Test(d: 4, c: 3, b: 2, a: 1); //1,2,3,4
Test(5, 6, c: 7, d: 8); //5,6,7,8
Console.ReadKey();
}
}
注意事项
·调用时命名参数要放在位置参数后面,不然会出错,例如:Test(d: 4, c: 3, 1, 2);
2.可选参数
复制内容到剪贴板
程序代码

static void Test(int a = 1, int b =2 , int c = 3, int d = 4)
{
Console.WriteLine("{0},{1},{2},{3}", a, b, c, d);
}
static void Main(string[] args)
{
Test(); //1,2,3,4
Test(5, 6); //5,6,3,4
Console.ReadKey();
}
{
Console.WriteLine("{0},{1},{2},{3}", a, b, c, d);
}
static void Main(string[] args)
{
Test(); //1,2,3,4
Test(5, 6); //5,6,3,4
Console.ReadKey();
}
注意事项
·声明时可选参数要放在必选参数后面,不然会出错,例如:static void Test(int a = 1, int b =2 , int c = 3, int d);
·调用时为某个可选参数传入实参,则其前面的所有参数都必须传入实参,不然会出错,例如:Test(, 6);
小结
命名参数和可选参数必须一起使用才能显示出优势。若没有可选参数,调用时所有参数必须传值,还不如不用命名参数;若没有命名参数,调用时为某个可选参数传入实参时,其前面所有参数都必须传值,可选参数变得没那么完美。
最后,再看一个例子:
复制内容到剪贴板
程序代码

class Program
{
static void Test(int a = 1, int b =2 , int c = 3, int d = 4)
{
Console.WriteLine("{0},{1},{2},{3}", a, b, c, d);
}
static void Main(string[] args)
{
Test(b: 8); //1,8,3,4
Console.ReadKey();
}
}
{
static void Test(int a = 1, int b =2 , int c = 3, int d = 4)
{
Console.WriteLine("{0},{1},{2},{3}", a, b, c, d);
}
static void Main(string[] args)
{
Test(b: 8); //1,8,3,4
Console.ReadKey();
}
}
参数资料
[1].命名实参和可选实参(C# 编程指南):http://msdn.microsoft.com/zh-cn/library/vstudio/dd264739.aspx






评论: 0 | 引用: 0 | 查看次数: 3692
发表评论
请登录后再发表评论!