C# 自己编写一个函数   string r(string a,string b)其中b为可选参数,如果没有b  则让b为一个特定字符  怎么写?  听说用函数重载就可以  但是我不会  求代码

解决方案 »

  1.   

    没有?string b =“”;
      

  2.   

    再写一个string r(string a)
      

  3.   

    string r(string a)
    {
       string b="特定的字符";
       ....
    }楼主是不是这个意思?
      

  4.   

     string r(string a,string b)
     string r(string a)
    {
      return r(a,"默认值");
    }
      

  5.   


    有的        private void funTest(params string[] values)
            {
                foreach (string s in values)
                {
                    MessageBox.Show(s);
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                funTest();
                funTest("1");
                funTest("1","2");
                funTest("1","2","a");
            }
      

  6.   

    C# 4.0 方法参数之可选参数
    static void DoSomething(int Arg,string arg1 = "default Arg1", string arg2 = "default arg2") {  
       Console.WriteLine("arg1 = {0},arg2 = {1}",arg1,arg2);  
     } 
      

  7.   

    Hi ZengHD学习了, 我想问下, 必须通过数组的方式来实现可选参数?这样具体应用中如果类型不相同, 怎么办?
      

  8.   


    Hi wuya11,
    没想到C# 4.0 开始支持了, 不错。
      

  9.   

    而在vb.net里,如果使用可选参数,则必须带有默认值。如 public sub testoptional(optional i as integer = 1) end sub 调用的时候,既可以写成testoptional(2),也可以写成testoptional(),这种情况参数i自动等于1。如果过程有不止一个可选参数,则vb还提供一种简化操作的方法——按名传递参数。比如过程 public sub testoptional(optional i as int32 = 1, optional j as int32 = 1, optional k as int32 = 1) end sub 如果只想指定k,让i和j使用默认值,就可以使用按名传递,如下 testoptional(k := 2) 而且这种方式不受参数表顺序的限制 testoptional(k := 2, i := 3, j := 5)