StringReader.ReadAsync 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| ReadAsync(Char[], Int32, Int32) | 异步从当前字符串中读取指定数目的字符并从指定索引开始将该数据写入缓冲区。 | 
| ReadAsync(Memory<Char>, CancellationToken) | 从当前位置开始异步读取输入字符串中的所有字符,并将当前位置移到输入字符串的末尾。 | 
ReadAsync(Char[], Int32, Int32)
- Source:
- StringReader.cs
- Source:
- StringReader.cs
- Source:
- StringReader.cs
异步从当前字符串中读取指定数目的字符并从指定索引开始将该数据写入缓冲区。
public:
 override System::Threading::Tasks::Task<int> ^ ReadAsync(cli::array <char> ^ buffer, int index, int count);public override System.Threading.Tasks.Task<int> ReadAsync (char[] buffer, int index, int count);[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task<int> ReadAsync (char[] buffer, int index, int count);override this.ReadAsync : char[] * int * int -> System.Threading.Tasks.Task<int>[<System.Runtime.InteropServices.ComVisible(false)>]
override this.ReadAsync : char[] * int * int -> System.Threading.Tasks.Task<int>Public Overrides Function ReadAsync (buffer As Char(), index As Integer, count As Integer) As Task(Of Integer)参数
- buffer
- Char[]
当此方法返回时,包含指定的字符数组,此数组中 index 和 (index + count - 1) 之间的值被从当前源中读取的字符所替换。
- index
- Int32
在 buffer 中开始写入的位置。
- count
- Int32
最多读取的字符数。 如果在写入指定数目的字符到缓冲区之前,就已经达到字符串的末尾,则方法返回。
返回
表示异步读取操作的任务。 
              TResult 参数的值包含读入缓冲区的总字节数。 如果当前可用字节数少于所请求的字节数,则该结果值可能小于所请求的字节数,或者如果已到达字符串的末尾时,则为 0(零)。
- 属性
例外
              buffer 为 null。
              index 或 count 为负数。
              index 和 count 的总和大于缓冲区长度。
字符串读取器已被释放。
以前的读取操作当前正在使用读取器。
示例
以下示例演示如何异步读取字符串的前 23 个字符。
using System;
using System.IO;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadCharacters();
        }
        static async void ReadCharacters()
        {
            string stringToRead = "Some characters to read but not all";
            char[] charsRead = new char[stringToRead.Length];
            using (StringReader reader = new StringReader(stringToRead))
            {
                await reader.ReadAsync(charsRead, 0, 23);
                Console.WriteLine(charsRead);
            }
        }
    }
}
// The example displays the following output:
// Some characters to read
//
Imports System.IO
Module Module1
    Sub Main()
        ReadCharacters()
    End Sub
    Async Sub ReadCharacters()
        Dim stringToRead = "Some characters to read but not all"
        Dim charsRead(stringToRead.Length) As Char
        Using reader As StringReader = New StringReader(stringToRead)
            Await reader.ReadAsync(charsRead, 0, 23)
            Console.WriteLine(charsRead)
        End Using
    End Sub
End Module
' The example displays the following output:
' Some characters to read
'
注解
读取参数指定的 count 字符数或到达字符串末尾后,任务完成。
此方法将存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅 引发的 Read(Char[], Int32, Int32)异常。
适用于
ReadAsync(Memory<Char>, CancellationToken)
- Source:
- StringReader.cs
- Source:
- StringReader.cs
- Source:
- StringReader.cs
从当前位置开始异步读取输入字符串中的所有字符,并将当前位置移到输入字符串的末尾。
public override System.Threading.Tasks.ValueTask<int> ReadAsync (Memory<char> buffer, System.Threading.CancellationToken cancellationToken = default);override this.ReadAsync : Memory<char> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>Public Overrides Function ReadAsync (buffer As Memory(Of Char), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)参数
- cancellationToken
- CancellationToken
要监视取消请求的标记。 默认值为 None。
返回
表示异步读取操作的任务。 
              TResult 参数的值包含读入缓冲区的总字符数。
例外
取消令牌已取消。 此异常存储在返回的任务中。