C#4.0引入了命名参数,它允许指定要使用哪个参数。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NamedPara { public static class WordProcessor { private const bool defaultval = false; public static List<string> GetWords( string sentence, bool capitalizwwords = false, bool reverseorder = defaultval, bool reverseWords = false ) { List<string> words = new List<string>(sentence.Split(' ')); return words; } } class Program { static void Main(string[] args) { string sentence = "can be assigned the values true false or null."; List<string> words; words = WordProcessor.GetWords(sentence); foreach (string s in words) { Console.Write(s); Console.Write(" "); } Console.Write("\n"); words = WordProcessor.GetWords(sentence, reverseWords: true, capitalizwwords:true); foreach (string s in words) { Console.Write(s); Console.Write(" "); } Console.Write("\n"); Console.ReadLine(); } } }