我写了一个函数,想返回一个int和一个string。一般怎么处理比较好?
解决方案 »
- [高分求助] C# 调用一个COM接口报出异常的问题
- 关于一个继承的问题
- c#小题
- 在Repeater的ItemCommand事件中如何获取对应的那行记录的值?
- [求助]設置控件的屬性A,屬性B也跟著變化并在PropertyGrid中呈現...愚翁老大請進!
- C#中listview怎么用?
- 为什么我在word中插入activeX保存后,再打开就变成了一个红叉,刚插入的时候挺正常的
- C# comboBox自动保存历史记录
- 请教:c#中,多人同时操作oracle数据库问题(急)
- 奇了怪了,C#中true和false也可以当作运算符重载,可是怎么用呢?
- (winform)如何知道一个窗口存活的时间?
- 求一条MySQL语句
2——object[] func()
方案2:利用out public int Method( out string result2 ){ }
int fun(ref string strkey)
我这样调用
sting s=null;
int i=fun(s);
编译出错:"无法从“string”转换为“ref string”
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){...}
这样调用
sting s=null;
int i=fun(ref s);
谢谢各位,顺便问一下ref和out的区别
{
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 关键字不能修饰返回值
将返回的对象封装成类或结构方法二:
将返回的对象用引用型对象
方法二:
将返回的对象用out参数
int fun(ref string strkey)
我这样调用
sting s=null;
int i=fun(s);
编译出错:"无法从“string”转换为“ref string”=======================================
当然啦,你的s=null嘛!
这样要用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 方面不同的重载。