列表作为“out”参数导致错误。为什么?

发布于 2024-11-09 06:46:02 字数 521 浏览 0 评论 0原文

在此代码中:

public bool SomeMethod(out List<Task> tasks)
{
    var task = Task.Factory.StartNew(() => Process.Start(info));
    tasks.Add(task);
}

我收到错误“使用未分配的输出参数‘任务’”。为什么?

在 MSDN 示例中,仅使用了 out 参数

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }

    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

,是因为 List 吗?

In this code:

public bool SomeMethod(out List<Task> tasks)
{
    var task = Task.Factory.StartNew(() => Process.Start(info));
    tasks.Add(task);
}

I get an error, "Use of unassigned out parameter 'tasks'". Why?

In an MSDN example there's just use of out parameter

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }

    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

Is it because of List<T>?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青衫负雪 2024-11-16 06:46:02

您必须在方法主体中初始化 out 参数(即创建一个新的 List 实例并将其分配给 out 参数):

public bool SomeMethod(out List<Task> tasks) {
  var task = Task.Factory.StartNew(() => Process.Start(info);
  tasks = new List<Task>() { task };
  ...
}

我使用集合初始值设定项语法将任务添加到列表中,但如果您愿意,也可以调用 Add 方法。

您应该这样调用该方法:

List<Task> tasks;
SomeMethod(out tasks);
var newTask = tasks[0]; // Access the task just created.

C# 7.0 引入了新的更简单的语法,您可以使用 out 参数在函数调用中声明变量:

SomeMethod(out var tasks);
var newTask = tasks[0]; // Access the task just created.

作为 List 通过引用传递,您可以去掉 out 参数。然后,您必须在调用该方法之前创建列表:

public bool SomeMethod(List<Task> tasks) {
  var task = Task.Factory.StartNew(() => Process.Start(info);
  tasks.Add(task);
  ...
}

并像这样调用它:

var tasks = new List<Task>();
SomeMethod(tasks);
var newTask = tasks[0]; // Access the task just created.

一般来说,避免使用 out 参数是一种很好的做法,因为它们可能会造成混淆。

You have to initialize the out parameter in the method body (that is create a new List<Task> instance and assign it to the out parameter):

public bool SomeMethod(out List<Task> tasks) {
  var task = Task.Factory.StartNew(() => Process.Start(info);
  tasks = new List<Task>() { task };
  ...
}

I'm using the collection initializer syntax to add the task to the list, but you could call the Add method instead if you prefer.

You should call the method like this:

List<Task> tasks;
SomeMethod(out tasks);
var newTask = tasks[0]; // Access the task just created.

C# 7.0 has introduced new simpler syntax where you declare the variable in the call to the function with the out parameter:

SomeMethod(out var tasks);
var newTask = tasks[0]; // Access the task just created.

As a List<T> is passed by reference you can get rid of the out parameter. You then have to create the list before calling the method:

public bool SomeMethod(List<Task> tasks) {
  var task = Task.Factory.StartNew(() => Process.Start(info);
  tasks.Add(task);
  ...
}

And call it like this:

var tasks = new List<Task>();
SomeMethod(tasks);
var newTask = tasks[0]; // Access the task just created.

In general it is good practice to avoid out parameters because they can be confusing.

夏见 2024-11-16 06:46:02

out 表示您的方法需要创建对象,然后将其分配给参数:

List<Task> tasks = new List<Task>();
tasks.Add(task);

out means that your method needs to create the object, then assign it to the parameter:

List<Task> tasks = new List<Task>();
tasks.Add(task);
追星践月 2024-11-16 06:46:02

这是因为您没有为 tasks 变量分配值...在本例中,该值将是对 List 类型实例的引用。

tasks = new List(); 添加到 SomeMethod 的主体中,一切都会正常工作:

public bool SomeMethod(out List<Task> tasks) {
   tasks = new List<Task>();
   var task = Task.Factory.StartNew(() => Process.Start(info);
   tasks.Add(task);
}

It's because you didn't assign a value to the tasks-variable... in this case that would be a reference to a instance of type List<Task>.

Add tasks = new List<Task>(); to the body of SomeMethod and everything will work fine:

public bool SomeMethod(out List<Task> tasks) {
   tasks = new List<Task>();
   var task = Task.Factory.StartNew(() => Process.Start(info);
   tasks.Add(task);
}
长不大的小祸害 2024-11-16 06:46:02

您需要初始化tasks参数。例如 tasks = new List()

此线程讨论了带有参数的outref 的使用。如果您使用ref关键字,那么您必须在调用该方法之前设置该值。不过,我认为在这种情况下没有理由使用 ref 关键字

You need to initialise the tasks parameter. e.g. tasks = new List<Task>()

This thread discusses the use of out and ref with parameters. If you use the ref keyword then you must have set the value prior to calling the method. I see no reason to use the ref keyword in this case though

绾颜 2024-11-16 06:46:02

您需要执行 tasks = new List(); 才能向其中添加 Task 对象。 MSDN 有一个更接近的示例对于你正在做的事情,这传递了一个数组而不是一个 int 。

You need to do tasks = new List<Task>(); before you can add a Task object to it. MSDN has an example that is closer to what you're doing, this passes an array rather than an int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文