亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在異步方法中使用 Span<byte>?

如何在異步方法中使用 Span<byte>?

C#
慕容森 2023-07-22 16:09:38
我正在嘗試使用以下代碼確定異步方法中有效的 base64 字符串:public static async Task<bool> IsBase64String(string base64){    Span<byte> buffer = new Span<byte>(new byte[base64.Length]);    return await Task.FromResult(Convert.TryFromBase64String(base64, buffer, out int bytesParsed));}但是 Span 不能在異步方法中聲明,請幫忙嗎?
查看完整描述

3 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

如果需要Span<T>async方法中使用 a并且可以使用 C# 7 或更高版本,那么您可以利用稱為本地函數的語言功能來解決錯誤Parameters or locals of type 'T' cannot be declared in async methods or lambda expressions。下面是一個如何做到這一點的示例......這再次是為了解決給定的原因。

using System;

using System.Text;

using System.Threading.Tasks;


public class Program

{

? ? public static async Task Main(string[] args)

? ? {

? ? ? ? var base64Str = Convert.ToBase64String(Encoding.UTF8.GetBytes("this is a Base64 str"));

? ? ? ? var notBase64Str = "this is not a Base64 str";

? ? ? ? Console.WriteLine(await IsBase64String(base64Str)); // True

? ? ? ? Console.WriteLine(await IsBase64String(notBase64Str)); // False

? ? }


? ? public static async Task<bool> IsBase64String(string base64)

? ? {

? ? ? ? return await Task.FromResult(CheckIfIsBase64String());

? ? ? ? // Note the use of a local non-async function so you can use `Span<T>`

? ? ? ? bool CheckIfIsBase64String()

? ? ? ? {

? ? ? ? ? ? // the use of stackalloc avoids array heap allocation

? ? ? ? ? ? Span<byte> buffer = stackalloc byte[base64.Length];

? ? ? ? ? ? return Convert.TryFromBase64String(base64, buffer, out int bytesParsed);

? ? ? ? }

? ? }

}


查看完整回答
反對 回復 2023-07-22
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

我正在嘗試確定異步方法中有效的 base64 字符串

為什么?該方法沒有任何異步之處。解決這個問題的正確方法是刪除關鍵字async

public static bool IsBase64String(string base64){
  Span<byte> buffer = new Span<byte>(new byte[base64.Length]);
    return Convert.TryFromBase64String(base64, buffer, out int bytesParsed);
}


查看完整回答
反對 回復 2023-07-22
?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

System.IO.Stream例如,如果您觀察方法,您將看到同步方法Stream.Read()和Stream.Write()acceptSpan<byte>以及異步方法Stream.ReadAsync()和Stream.WriteAsync()accept Memory<byte>。因此,如果您使用真正的異步方法,則不需要創建Span<>.


在您的情況下,您不需要將結果包裝在Task. 該操作將同步完成并寫入Task. 因此,當您將其包裝在Taskand awaitit 中時,您實際上是在說“將這個結果包裝在任務中,然后給我任務的結果?!薄?/p>


但如果您確實想Task從方法中返回,則可以刪除async和await關鍵字。該方法仍然可以從外部等待。(再次強調:在這種情況下,無需將結果包裝在 中Task)


// Removed async await

public static Task<bool> IsBase64StringAsync(string base64)

{

    Span<byte> buffer = new Span<byte>(new byte[base64.Length]);

    return Task.FromResult(Convert.TryFromBase64String(base64, buffer, out int bytesParsed));

}


// Synchronous method

public static bool IsBase64String(string base64)

{

    Span<byte> buffer = new Span<byte>(new byte[base64.Length]);

    return Convert.TryFromBase64String(base64, buffer, out int bytesParsed);

}


查看完整回答
反對 回復 2023-07-22
  • 3 回答
  • 0 關注
  • 381 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號