鉴于以下C#代码:
public class Info
{
public string Status { get; set; } = "Init";
}
public static class InfoHelper
{
public static async Task UpdateStatusV1(Func<Info> getInfo)
{
Info someInfo = getInfo();
someInfo.Status = "Checking...";
await Task.Delay(1000); // simulating complicated and important work
someInfo.Status = "Updated";
}
public static async Task UpdateStatusV2(Info someInfo)
{
someInfo.Status = "Checking...";
await Task.Delay(1000); // simulating complicated and important work
someInfo.Status = "Updated";
}
}
public static async Task UpdateStatusV3(Func<Info> getInfo)
{
await DoComplicatedAndImportantWorkV1();
async Task DoComplicatedAndImportantWorkV1()
{
Info someInfo = getInfo();
await Task.Delay(1000); // simulating complicated and important work
someInfo.Status = "Updated";
}
}
public static async Task UpdateStatusV4(Info someInfo)
{
await DoComplicatedAndImportantWorkV2();
async Task DoComplicatedAndImportantWorkV2()
{
await Task.Delay(1000); // simulating complicated and important work
someInfo.Status = "Updated";
}
}
InfoHelper.UpdateStatusV1在线程安全方面是否优于InfoHelper.UpdateStatusV2?
InfoHelper.UpdateStatusV1“ someInfo ”对象使用和修改比InfoHelper.UpdateStatusV2更安全吗?
InfoHelper.UpdateStatusV3在线程安全方面优于InfoHelper.UpdateStatusV4 - 例如维护调用方法的“ someInfo ”“线程上下文” ?
答案 0 :(得分:1)
没有任何有用的区别。时间函数更有用:
await
在这种情况下:与直接传递值相比没有有用的区别。还有很多方法可以引入错误。