Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步C方法不返回字符串_C#_Visual Studio_Function_Asynchronous_Methods - Fatal编程技术网

C# 异步C方法不返回字符串

C# 异步C方法不返回字符串,c#,visual-studio,function,asynchronous,methods,C#,Visual Studio,Function,Asynchronous,Methods,我创建了一个返回字符串值的函数,但得到的错误是: 异步方法的返回类型必须为void、Task或Task 代码如下: private async string SerialRead() { const uint maxReadLength = 1024; DataReader dataReader = new DataReader(SerialPort.InputStream); uint bytesToRead = await dataReader.LoadAsync(m

我创建了一个返回字符串值的函数,但得到的错误是:

异步方法的返回类型必须为void、Task或Task

代码如下:

private async string SerialRead()
{
    const uint maxReadLength = 1024;
    DataReader dataReader = new DataReader(SerialPort.InputStream);
    uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
    string rxBuffer = dataReader.ReadString(bytesToRead);
    return rxBuffer;
}
试试这个

private async Task<string> SerialRead()
{
    const uint maxReadLength = 1024;
    DataReader dataReader = new DataReader(SerialPort.InputStream);
    uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
    string rxBuffer = dataReader.ReadString(bytesToRead);
    return rxBuffer;
}  

错误消息清晰、直接且正确。可能重复