C#4.0参数默认值

来源:互联网 发布:灰鸽子源码 编辑:程序博客网 时间:2024/04/29 21:54
[csharp] view plaincopy
  1. static void Main(string[] args)  
  2. {  
  3.     Console.WriteLine("4.0默认参数:");  
  4.     SayHello("Tom",20);  
  5.     SayHello("Jim");//调用时可以不写有默认值的参数  
  6.   
  7.     Console.WriteLine("旧版用重载实现:");  
  8.     SayHi("Tom", 20);  
  9.     SayHi("Jim");  
  10.   
  11.     Console.ReadKey();  
  12. }  
  13. //参数默认值  
  14. static void SayHello(string name,int age=18)  
  15. {  
  16.     Console.WriteLine("Hello, I am {0}, I am {1} years old.",name,age);  
  17. }  
  18. //这里用两个重载的方法实现了参数默认值  
  19. static void SayHi(string name)  
  20. {  
  21.     Console.WriteLine("Hi, I am {0}, I am {1} years old.", name, 18);  
  22. }  
  23. static void SayHi(string name,int age)  
  24. {  
  25.     Console.WriteLine("Hi, I am {0}, I am {1} years old.", name, age);  
  26. }  
0 0
原创粉丝点击