翻译:Visual C# 4.0中的新特性-第一部分-可选参数(Optional parameters)
2009-12-03 23:18
316 查看
This is the first blog from a series of blog post which I'm planning to do on whet’s new in Visual C# 4.0
这是我打算写的《Visual C# 4.0中的新特性》系列文章的第一篇。
可选参数(Optional parameters)
Optional parameters is a new feature in C# 4.0 which will let you set a default value for an argument of a method. In case that the collie of the method will omit the argument the default value will take its place.
可选参数(Optional parameters)是C# 4.0的一个新特性,可以为一个方法的参数设置一个默认值。(译者注:为一个参数设置一个默认值,这个参数就是可选参数)一旦被调用的方法忽略可选参数,就会用默认值代替。
So instead of writing the following code:
所以,替换下面的代码:
代码
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 SaySomething();
6 SaySomething("Ohad");
7
8 Console.ReadLine();
9 }
10
11 public static void SaySomething(string name = string.Empty)
12 {
13 Console.WriteLine(string.Format("Hi {0}!",name));
14 }
15 }
16
using string.Empty will result with a compilation error:
用string.Empty将引起一个编译错误:
“Default parameter value for 'name' must be a compile-time constant”
“'name'的默认值应该是一个编译时常量”
And as it string.Empty is not a literal.
而string.Empty不是一个字面值。
这是我打算写的《Visual C# 4.0中的新特性》系列文章的第一篇。
可选参数(Optional parameters)
Optional parameters is a new feature in C# 4.0 which will let you set a default value for an argument of a method. In case that the collie of the method will omit the argument the default value will take its place.
可选参数(Optional parameters)是C# 4.0的一个新特性,可以为一个方法的参数设置一个默认值。(译者注:为一个参数设置一个默认值,这个参数就是可选参数)一旦被调用的方法忽略可选参数,就会用默认值代替。
So instead of writing the following code:
所以,替换下面的代码:
代码
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 SaySomething();
6 SaySomething("Ohad");
7
8 Console.ReadLine();
9 }
10
11 public static void SaySomething(string name = string.Empty)
12 {
13 Console.WriteLine(string.Format("Hi {0}!",name));
14 }
15 }
16
using string.Empty will result with a compilation error:
用string.Empty将引起一个编译错误:
“Default parameter value for 'name' must be a compile-time constant”
“'name'的默认值应该是一个编译时常量”
And as it string.Empty is not a literal.
而string.Empty不是一个字面值。
相关文章推荐
- 翻译:Visual C# 4.0的新特性-第二部分-命名参数(Names Parameters)
- 翻译:Visual C# 4.0的新特性-第三部分-Dynamic ExpendoObject
- 精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数, 动态绑定(dynamic), 泛型协变和逆变, CountdownEvent, Barrier
- C# 4.0 新特性dynamic、可选参数、命名参数等
- 精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数, 动态绑定(dynamic), 泛型协变和逆变, CountdownEvent, Barrier
- C# 4.0 新特性dynamic、可选参数、命名参数等
- C# 4.0 新特性dynamic、可选参数、命名参数等
- 精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数, 动态绑定(dynamic), 泛型协变和逆变, CountdownEvent, Barrier
- C# 4.0 新特性dynamic、可选参数、命名参数等
- C# 4.0 新特性dynamic、可选参数、命名参数等
- 精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数
- C# 4.0 特性: Named & Optional Parameters(可选和命名参数)
- Visual C# 2010新特性之命名和可选参数与类型等价支持
- C# 4.0 新特性dynamic、可选参数、命名参数等 分类: .NET 2013-07-26 11:15 414人阅读 评论(0) 收藏
- C# 4.0 新特性dynamic、可选参数、命名参数等
- 深入C# 4.0 新特性dynamic、可选参数、命名参数的详细介绍
- C# 4.0 新特性dynamic、可选参数、命名参数等
- C#4.0新特性:可选参数,命名参数,Dynamic
- C#方法笔记一:C#4.0新特性:命名参数和可选参数
- C# 4.0 新特性dynamic、可选参数、命名参数等