StringReader.ReadAsync 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| ReadAsync(Char[], Int32, Int32) |
非同步讀取目前字串指定的最大字元數,並將資料寫入緩衝區,從指定的索引開始。 |
| ReadAsync(Memory<Char>, CancellationToken) |
非同步讀取輸入字串中的所有字元,從當前位置開始,並將當前位置推進到輸入字串的末尾。 |
ReadAsync(Char[], Int32, Int32)
非同步讀取目前字串指定的最大字元數,並將資料寫入緩衝區,從指定的索引開始。
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)
非同步讀取輸入字串中的所有字元,從當前位置開始,並將當前位置推進到輸入字串的末尾。
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 值包含讀取到緩衝區的總字元數。
例外狀況
取消令牌也被取消了。 此例外會儲存在回傳的任務中。