繁体   English   中英

我不知道如何填充列表<string>在 c# 任务中正确</string>

[英]I can't figure out how to populate a List<string> inside a c# Task correctly

我写这个荒谬的代码只是为了弄清楚:

  • 如果我使用CallingFunctionAsync_v1CallingFunctionAsync_v2有区别吗?

如果是这样,有人可以解释为什么吗?

定义:

class WorkHorse
{
    const int RECORD_SIZE = 12;

    public async Task<List<string>> DoTheThingAsync_v1(CancellationToken cancellationToken)
    {

        List<string> theList = new List<string>();

        byte[] buffer = new byte[RECORD_SIZE];

        using var fs = File.OpenRead("awesome_stuff.bin");
        int read;
        while ((read = await fs.ReadAsync(buffer, 0, RECORD_SIZE, cancellationToken).ConfigureAwait(false)) != 0)
        {
            theList.Add(Encoding.UTF8.GetString(buffer, 0, RECORD_SIZE));
        }

        return theList;
    }

    public async Task DoTheThingAsync_v2(List<string> theList, CancellationToken cancellationToken)
    {
        byte[] buffer = new byte[RECORD_SIZE];

        using var fs = File.OpenRead("awesome_stuff.bin");
        int read;
        while ((read = await fs.ReadAsync(buffer, 0, RECORD_SIZE, cancellationToken).ConfigureAwait(false)) != 0)
        {
            theList.Add(Encoding.UTF8.GetString(buffer, 0, RECORD_SIZE));
        }
    }
}

用法:

class FunTimes
{
    List<string> TheStrings;

    public async Task CallingFunctionAsync_v1(CancellationToken cancellationToken)
    {
        this.TheStrings = await new WorkHorse().DoTheThingAsync_v1(cancellationToken).ConfigureAwait(false);
    }

    public Task CallingFunctionAsync_v2(CancellationToken cancellationToken)
    {
        this.TheStrings = new List<string>();
        return new WorkHorse().DoTheThingAsync_v2(this.TheStrings, cancellationToken);
    }

}

V1 是更好的方法。 它是一个纯粹的黑盒 function,它总是返回一个新列表。

V2 需要一个List<string>的初始化实例,这很容易出错。 您可以轻松通过 null 并遇到问题。 然后,由于这些是异步方法,您可以同时运行 V2,并且您将在已通过的列表上出现竞争条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM