C#基础:可选参数和命名参数

        可选参数:调用参数时,常常需要给方法传参数,例如public List<string> GetValue(string str,bool b1,boolb2){},str是必须参数,如果给b1,b2提供一个默认值,就可以把b1,b2变成可选参数,调用方法时,如果不给b1,b2传值,就使用默认值。默认值必须是字面值,常量或者默认值类型的值。可选参数必须位于方法参数列表末尾。

        命名参数:使用可选参数时,可以指定要使用的可选参数。

       //命名参数和可选参数

  1. class WordProcessor
  2. {
  3.                                                                                                   //可选参数1,                        可选参数2                       可选参数3
  4.         public static List<string> GetWords (string sentence,bool capitalizeWords=false,bool reverseOrder=false,bool reverseWords=false){
  5.             List<string> words = new List<string>(sentence.Split(' '));
  6.             if (capitalizeWords)//正常输出
  7.             {
  8.                 words = CapitalizeWords(words);
  9.             }
  10.             if (reverseOrder)//单词反向输出
  11.             {
  12.                 words = ReverseOrder(words);
  13.             }
  14.             if (reverseWords)//全部反向输出
  15.             {
  16.                 words = ReverseWords(words);
  17.             }
  18.             return words;
  19.         }
  20.        //按顺序,首字母大写,正常输出文本
  21.         public static List<string> CapitalizeWords(List<string> words) {
  22.             List<string> capw = new List<string>();
  23.             foreach (var word in words)
  24.             {
  25.                 if (word.Length==0)
  26.                 {
  27.                     continue;
  28.                 }
  29.                 if (word.Length == 1)
  30.                 {
  31.                     capw.Add(word[0].ToString().ToUpper());
  32.                 }
  33.                 else {
  34.                     capw.Add(word[0].ToString().ToUpper()+word.Substring(1));
  35.                 }
  36.             }
  37.             return capw;
  38.         }
  39.         //单词反向输出
  40.         public static List<string> ReverseOrder(List<string> words) {
  41.             List<string> revo = new List<string>();
  42.             for (int wordIndex = words.Count-1; wordIndex>=0; wordIndex--)
  43.             {
  44.                 revo.Add(words[wordIndex]);
  45.             }
  46.             return revo;
  47.         }
  48.         //全部反向输出
  49.         public static List<string> ReverseWords(List<string> words) {
  50.             List<string> revw = new List<string>();
  51.             foreach (var word in words)
  52.             {
  53.                 revw.Add(ReverseWord(word));
  54.             }
  55.             return revw;
  56.         }
  57.         public static string ReverseWord(string word) {
  58.             StringBuilder sb = new StringBuilder();
  59.             for (int characterInder = word.Length-1; characterInder>=0; characterInder--)
  60.             {
  61.                 sb.Append(word[characterInder]);
  62.             }
  63.             return sb.ToString();
  64.         }
  65.     }
  66. public static void Main(string[] args){
  67.             string sentence = "his gaza against the sweeping bars has" + "grown so weary";
  68.             List<string> words;
  69.             Console.WriteLine("正常文本:");//只有1个必选参数
  70.             words = WordProcessor.GetWords(sentence);       
  71.             foreach (var word in words)
  72.             {
  73.                 Console.Write(word);
  74.                 Console.Write(' ');
  75.             }
  76.             Console.WriteLine();
  77.             Console.WriteLine("有可选参数的文本:");//1个必选参数,2个可选参数
  78.             words = WordProcessor.GetWords(sentence, capitalizeWords: true, reverseOrder: true);
  79.             foreach (var word in words)
  80.             {
  81.                 Console.Write(word);
  82.                 Console.Write(' ');
  83.             }
  84.             Console.WriteLine();
  85. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值