写一个函数,要返回多个值,应该怎么样做?

TreelightLSH 2003-08-12 02:41:34
最好给出例子.
...全文
119 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
鸡毛掸子尘埃 2003-08-13
  • 打赏
  • 举报
回复
返回一个Object。
TreelightLSH 2003-08-13
  • 打赏
  • 举报
回复
问题已解决了.
TreelightLSH 2003-08-13
  • 打赏
  • 举报
回复
"控制离开当前方法之前必须对输出参数赋值"这个问题怎样解决?
dahuzizyd 2003-08-12
  • 打赏
  • 举报
回复
帮助里关于输出参数的说明:

用 out 修饰符声明的参数是输出参数。类似于引用参数,输出参数不创建新的存储位置。相反,输出参数表示与在方法调用中作为参数给出的变量相同的存储位置。

当形参为输出参数时,方法调用中的相应参数必须包含关键字 out,后跟与形参类型相同的变量引用(第 5.4 节)。变量在可以作为输出参数传递之前不需要明确赋值,但是在将变量作为输出参数传递的调用之后,变量被认为是明确赋值的。

在方法内部,与局部变量相同,输出参数最初被认为是未赋值的,因而必须在使用它的值之前明确赋值。

在方法返回之前,方法的每个输出参数都必须明确赋值。

输出参数通常用在产生多个返回值的方法中。例如:

class Test
{
static void SplitPath(string path, out string dir, out string name)
{
int i = path.Length;
while (i > 0)
{
char ch = path[i – 1];
if (ch == '\\' || ch == '/' || ch == ':') break;
i--;
}
dir = path.Substring(0, i);
name = path.Substring(i);
}
static void Main()
{
string dir, name;
SplitPath("c:\\Windows\\System\\hello.txt", out dir, out name);
Console.WriteLine(dir);
Console.WriteLine(name);
}
}
此例产生下列输出:

c:\Windows\System\
hello.txt
_weiKun_ 2003-08-12
  • 打赏
  • 举报
回复
利用输出参数和结构,类也可一,和结构差不多。



ddy2000 2003-08-12
  • 打赏
  • 举报
回复
用数组作为返回值
public static string[,] GetGoal(string strName,int iYear)
{
// 定义
string[,] strReturn=new string[6,3];
for(int i=0;i<6;i++)
{
for(int j=0;j<3;j++)
{
strReturn[i,j]="";
}
}
try
{
// 查询语句
}
catch
{
// 出现错误
return null;
}

// 返回数组
return strReturn;
}

amagic 2003-08-12
  • 打赏
  • 举报
回复
利用输出参数out,它专门用来为函数传递返回数据
void test( out int a, out char b, ... )
{
a = 1;
b = 'b';
...
}
panyee 2003-08-12
  • 打赏
  • 举报
回复
ref 的参数, 对象的话,在进入函数前要实例化

out参数,传入时可以是空的, 但传出时必须进行赋值操作,不管是不是给空值
panyee 2003-08-12
  • 打赏
  • 举报
回复
public int MyMethod(ref int a, ref stirng b, out string c)
{
...
}

用引用参数 ref和out, 这样, 你此函数可以得到4个返回值

saucer 2003-08-12
  • 打赏
  • 举报
回复
or use out, ref, or structure

int GetValue(out int s, ref int s2)
{
s = s2 + 1;
s2 = 12;
return s+ s2;
}



int i;
int j=2;
int n = GetValue(out i, ref j);



ETstudio 2003-08-12
  • 打赏
  • 举报
回复
也可以这样的
private string aaa(string aaa,out string bbb,ref string ccc)
{

}
好运 2003-08-12
  • 打赏
  • 举报
回复
gz
顾君彦 2003-08-12
  • 打赏
  • 举报
回复
用结构做为返回参数.
戓者使用ref关键字.
public struct param
{
public int i;
public string s;
}
public param aaa()
{
param aa = new param();
aa.i =10;
aa.s ="sdfsdf";
return aa;
}

111,096

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧