我有一个方法,我想使用out参数。但我错过了一些我找不到的东西。我有3个参数,第一个是长id,我正在发送这个ID,我正在处理它,我正在创建我的workerName (第二个参数)和workerTitle (第三个参数)。我的方法是;
public static void GetWorkerInfo( long workerID, out string workerName, out string workerTitle)
{
// Some code here
}在那里我呼唤我的方法;
GetWorkerInfo(workerID, out workerName, out workerTitle)发布于 2017-03-20 08:02:55
使用C# 7,您可以将输出参数声明为方法调用的一部分,如下所示:
GetWorkerInfo(workerID, out var workerName, out var workerTitle);但是,在切换到C# 7之前,必须声明调用之外作为out参数传递的变量:
string workerName;
string workerTitle;
GetWorkerInfo(workerID, out workerName, out workerTitle);发布于 2017-03-20 08:02:11
public static void GetWorkerInfo(long workerID, out string workerName, out string workerTitle)
{
workerName = "";
workerTitle = "";
}那就这样叫吧
long workerID = 0;
string workerTitle;
string workerName;
GetWorkerInfo(workerID, out workerName, out workerTitle);发布于 2017-03-20 08:04:31
此错误是因为您没有为指定为out参数的参数分配任何值。请记住,您应该在方法的主体内为这些参数分配一些值。
public static void GetWorkerInfo(long workerID, out string workerName, out string workerTitle)
{
workerName = "Some value here";
workerTitle = "Some value here also";
// rest of code here
}现在,您可以看到代码编译没有任何问题。
https://stackoverflow.com/questions/42898388
复制相似问题