|
- static async Task Main()
- {
- string resultString = await DelayResult<string>(2000, "Hello, World!");
- Console.WriteLine(resultString);
- // 示例:延迟1秒后返回整数 42
- int resultInt = await DelayResult<int>(1000, 42);
- Console.WriteLine(resultInt);
- }
- // 泛型方法 DelayResult<T>
- static async Task<T> DelayResult<T>(int delayMilliseconds, T value)
- {
- // 延迟指定的时间
- await Task.Delay(delayMilliseconds);
- // 返回指定的值
- return value;
- }
复制代码
c# 异步泛型实例
|
|