C# 无效参数";“出去”;参数

C# 无效参数";“出去”;参数,c#,visual-studio-2010,out,C#,Visual Studio 2010,Out,我有一个方法,我想使用out参数。但是我错过了一些我找不到的东西。我有3个参数,第一个是长id,我发送那个id,我处理它,我创建我的workerName(第二个参数)和workerTitle(第三个参数)。 我的方法是 public static void GetWorkerInfo( long workerID, out string workerName, out string workerTitle) { // Some code here } 我在哪里调用我的方法 GetWork

我有一个方法,我想使用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)
那就这样说吧

long workerID = 0;
string workerTitle;
string workerName;
GetWorkerInfo(workerID, out workerName, out workerTitle);
那就这样说吧

long workerID = 0;
string workerTitle;
string workerName;
GetWorkerInfo(workerID, out workerName, out workerTitle);
使用C#7,您可以将输出参数声明为方法调用的一部分,如下所示:

GetWorkerInfo(workerID, out var workerName, out var workerTitle);
但是,在切换到C#7之前,必须声明在调用之外作为
out
参数传递的变量:

string workerName;
string workerTitle;
GetWorkerInfo(workerID, out workerName, out workerTitle);
使用C#7,您可以将输出参数声明为方法调用的一部分,如下所示:

GetWorkerInfo(workerID, out var workerName, out var workerTitle);
但是,在切换到C#7之前,必须声明在调用之外作为
out
参数传递的变量:

string workerName;
string workerTitle;
GetWorkerInfo(workerID, out workerName, out workerTitle);

错误是因为没有为指定为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
}

现在您可以看到代码编译时没有出现任何问题。

错误是因为您没有为那些指定为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
}

现在,您可以看到代码编译没有任何问题。

在调用方法之前,您的“workerName”和“workerTitle”是如何定义的?您是否遇到此错误?在控件离开当前控件之前,必须将out参数“workerTitle”指定给它method@Jure谢谢这就是为什么我得到了例外@J.SMTBCJ15谢谢你,你呢“工作名称”和“工作时间”“在调用方法之前定义?是否出现此错误?在控件离开当前控件之前,必须将out参数'workertile'指定给它?”method@Jure谢谢,这就是为什么我得到了例外@J.SMTBCJ15谢谢你,谢谢你,事实上这是我第一次用完它,这就是为什么我错过了它。再次感谢你,非常感谢,事实上这是我第一次用完它,这就是我错过它的原因。再次感谢你