我写了一个函数,想返回一个int和一个string。一般怎么处理比较好?

解决方案 »

  1.   

    1——func(ref int param1,ref string param2)
    2——object[] func()
      

  2.   

    方案1: 把两个返回值包装成一个对象,返回对象.
    方案2:利用out  public int Method( out string result2 ){  }
      

  3.   

    只能依靠out 或者ref 参数了
      

  4.   

    在定义函数的时候加参数 output 就可以有返回值了
      

  5.   

    out  public int Method( out string result2 )
      

  6.   

    回JadyWang,我能不能这样定义函数
    int fun(ref string strkey)
    我这样调用
    sting s=null;
    int i=fun(s);
    编译出错:"无法从“string”转换为“ref string”
      

  7.   

    1、返回数组
    public object[] Method(){...}2、传入数组,在方法内部赋值
    public void Method(object[] par){...}3、用 ref 参数
    public int Method(ref string par){...}
    public string Method(ref int par){}
    public void Method(ref int par1,ref string par2){...}4、用 out 参数
    public int Method(out string par){...}
    public string Method(out int par){}
    public void Method(out int par1,out string par2){...}
      

  8.   

    int fun(ref string strkey)
    这样调用
    sting s=null;
    int i=fun(ref s);
      

  9.   

    问题我解决了,调用时用fun(ref s);
    谢谢各位,顺便问一下ref和out的区别
      

  10.   

    public class Result // struct Result
    {
         public int Result1;
         public string Result2;
    }1.使用结构体或类封装返回值:
    public Result Method();2.使用结构体或类封装参数,以修改 r 参数的 Result1 和 Result2 成员的方式返回值:
    public void Method(Result r);3.使用 out 或 ref 参数(M$ 不推荐使用 out 和 ref 关键字):
    public void Method(out int result1, out string result2);
    public int Method(out string result2);
    public string Method(out int result1);
    BTW 纠正一下:
    // 回复人: jianyi0115() ( ) 信誉:100  2005-10-21 11:10:00  得分: 0  
    // 方案1:把两个返回值包装成一个对象,返回对象.
    // 方案2:利用out  public int Method( out string result2 ){  }out 关键字不能修饰返回值
      

  11.   

    方法一:
    将返回的对象封装成类或结构方法二:
    将返回的对象用引用型对象
    方法二:
    将返回的对象用out参数
      

  12.   

    回JadyWang,我能不能这样定义函数
    int fun(ref string strkey)
    我这样调用
    sting s=null;
    int i=fun(s);
    编译出错:"无法从“string”转换为“ref string”=======================================
    当然啦,你的s=null嘛!
    这样要用out。
      

  13.   

    ref和out的区别:ref在使用前必须初始化,而out则不需要,但必须在函数中给他赋值.
    方法参数上的 ref 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。ref 参数的值被传递到 ref 参数。传递到 ref 参数的参数必须最先初始化。将此方法与 out 参数相比,后者的参数在传递到 out 参数之前不必显式初始化。属性不是变量,不能作为 ref 参数传递。如果两种方法的声明仅在它们对 ref 的使用方面不同,则将出现重载。但是,无法定义仅在 ref 和 out 方面不同的重载。
    方法参数上的 out 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。当希望方法返回多个值时,声明 out 方法非常有用。使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。若要使用 out 参数,必须将参数作为 out 参数显式传递到方法。out 参数的值不会传递到 out 参数。不必初始化作为 out 参数传递的变量。然而,必须在方法返回之前为 out 参数赋值。属性不是变量,不能作为 out 参数传递。如果两个方法的声明仅在 out 的使用方面不同,则会发生重载。不过,无法定义仅在 ref 和 out 方面不同的重载。