关于c# 默认参数
来源:互联网 发布:淘宝店铺招牌图片女装 编辑:程序博客网 时间:2024/06/11 07:18
#是不支持 默认参数的,今天在网上找了点资料,现在整理了;
模拟默认参数 :
源地址:http://msdn.microsoft.com/zh-cn/7s3cbse0(VS.80).aspx
class MyClass
{
static string myMethod(string precip, string country, string location)
{
return string.Format("The {0} in {1} stays mainly in the {2}.",
precip, country, location );
}
static string myMethod(string precip, string country )
{
return myMethod(precip, country, "plain");
}
static string myMethod()
{
return myMethod("rain", "Spain", "plain");
}
static void Main(string[] args)
{
System.Console.WriteLine(myMethod());
System.Console.WriteLine(myMethod("snow", "Walla Walla"));
}
}
{
static string myMethod(string precip, string country, string location)
{
return string.Format("The {0} in {1} stays mainly in the {2}.",
precip, country, location );
}
static string myMethod(string precip, string country )
{
return myMethod(precip, country, "plain");
}
static string myMethod()
{
return myMethod("rain", "Spain", "plain");
}
static void Main(string[] args)
{
System.Console.WriteLine(myMethod());
System.Console.WriteLine(myMethod("snow", "Walla Walla"));
}
}
C#: 方法的默认参数 :
源地址:http://www.cnblogs.com/hush/articles/4355.html
大家都知道在C++中,我们可以为方法的参数指定一个默认值,像这样:
void foo(int i = 100);
当我们以这种形式调用方法的时候: foo(); 实际上参数i被赋于了默认值,所以相当于调用了foo(100);
然而在C#中是不支持参数的默认值的,那么如果我们要用到类似的功能应该怎么实现呢?考虑下面这个例子:
class Buffer
{
public Buffer(int bufferSize = 100) //Compile Error
{
buf = new int [bufferSize];
}
private int[] buf;
}
首先当然要为Buffer提供一个无参的构造函数重载:
class Buffer
{
public Buffer(int bufferSize)
{
buf = new int[bufferSize];
}
public Buffer():this(100)
{
}
private int[] buf;
}
但这个方法有一个问题就是我们把Buffer的默认大小hard-coding到了代码里,这有两个弊端,一是损害了代码的可读性,二是用以上方法,如果Buffer有多个重载的构造函数都用到bufferSize的默认值,一旦你要修改默认值的大小,不得不同时修改多处程序,一旦漏掉了其中的一个,说不定就麻烦大了。
所以,正确的方法是为bufferSize提供一个const的默认值:
class Buffer
{
private const int defaultBufferSize = 100;
public Buffer(int bufferSize)
{
buf = new int[bufferSize];
}
public Buffer():this(defaultBufferSize)
{
}
private int[] buf;
}
观察编译器为public Buffer()产生的il代码
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 20 (0x14)
.maxstack 2
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ldarg.0
IL_0007: ldc.i4.s 100 //100即为defaultBufferSize的值
IL_0009: newarr [mscorlib]System.Int32
IL_000e: stfld int32[] Buffer::buf
IL_0013: ret
} // end of method Buffer::.ctor
defaultBufferSize的值在相应的调用处被替换成了字面常量(这其实也就是const成员的特性),所以使用defaultBufferSize不会影响public Buffer()的执行效率。而由于const成员隐含了static的特性,所以一个Buffer类只有一个defaultBufferSize的变量,性能的影响也是很小的。
我们可以看到.net 类库中的许多类都使用了这种方法
void foo(int i = 100);
当我们以这种形式调用方法的时候: foo(); 实际上参数i被赋于了默认值,所以相当于调用了foo(100);
然而在C#中是不支持参数的默认值的,那么如果我们要用到类似的功能应该怎么实现呢?考虑下面这个例子:
class Buffer
{
public Buffer(int bufferSize = 100) //Compile Error
{
buf = new int [bufferSize];
}
private int[] buf;
}
首先当然要为Buffer提供一个无参的构造函数重载:
class Buffer
{
public Buffer(int bufferSize)
{
buf = new int[bufferSize];
}
public Buffer():this(100)
{
}
private int[] buf;
}
但这个方法有一个问题就是我们把Buffer的默认大小hard-coding到了代码里,这有两个弊端,一是损害了代码的可读性,二是用以上方法,如果Buffer有多个重载的构造函数都用到bufferSize的默认值,一旦你要修改默认值的大小,不得不同时修改多处程序,一旦漏掉了其中的一个,说不定就麻烦大了。
所以,正确的方法是为bufferSize提供一个const的默认值:
class Buffer
{
private const int defaultBufferSize = 100;
public Buffer(int bufferSize)
{
buf = new int[bufferSize];
}
public Buffer():this(defaultBufferSize)
{
}
private int[] buf;
}
观察编译器为public Buffer()产生的il代码
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 20 (0x14)
.maxstack 2
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ldarg.0
IL_0007: ldc.i4.s 100 //100即为defaultBufferSize的值
IL_0009: newarr [mscorlib]System.Int32
IL_000e: stfld int32[] Buffer::buf
IL_0013: ret
} // end of method Buffer::.ctor
defaultBufferSize的值在相应的调用处被替换成了字面常量(这其实也就是const成员的特性),所以使用defaultBufferSize不会影响public Buffer()的执行效率。而由于const成员隐含了static的特性,所以一个Buffer类只有一个defaultBufferSize的变量,性能的影响也是很小的。
我们可以看到.net 类库中的许多类都使用了这种方法
做个收藏!
0 0
- 关于c# 默认参数
- c# 传递默认参数
- C#默认参数
- 关于默认参数
- C#默认参数和命名参数
- C# 方法参数(默认参数)
- C# 方法参数(默认参数)
- C#中默认参数的解决方法
- C#中默认参数的解决方法
- C#4.0支持默认参数了
- 关于引用类型的默认参数
- C++中关于默认构造函数和默认参数…
- C# 4.0新特性之命名参数,默认参数
- C# 关于默认打印纸张的设置
- 关于C#命令行参数教程
- 默认参数
- 默认参数
- 默认参数
- 匿名内部类总结
- 调皮的黑客 教你如何攻击亚马逊无人机
- 2014九月十月百度,迅雷,华为,阿里巴巴,最新校招笔试面试题
- 心得开始
- LeetCode Length of Last Word最后一个单词的长度
- 关于c# 默认参数
- 在WORD中使用宏达到 找到指定某一位置的内容后 做相关操作。
- 让bat文件后台运行
- 批处理隐藏运行的10种思路
- Win32_NetworkAdapterConfiguration
- VBS枚举出这个类的所有属性
- VMware6 下安装Fedora 14
- bad ELF interpreter: No such file or directory
- Google无人驾驶汽车与机器人组合,秒杀无人机