Looking at the implementation of CancellationToken.None, it is simply returning default(CancellationToken). However, I see no reference in CancellationToken's documentation that the two are equivalent.
I'd like to offer an API like this but not until I'm sure it'll always work:

Task DoSomething(CancellationToken token = default(CancellationToken))

Is it defined behavior that default(CancellationToken) is the same as CancellationToken.None, or is this just an implementation detail?

 

 

答1


 

They are the same. Check source code

public static CancellationToken None
{
    get { return default(CancellationToken); }
}

From https://referencesource.microsoft.com/#mscorlib/system/threading/CancellationToken.cs,36b17ded8b1a228c

 

 

答2


 

After filing an issue with corefx, the documentation remarks have been updated to make this a guaranteed feature:

You can also use the C# default(CancellationToken) statement to create an empty cancellation token.

 

 

原文链接

 

相关文章:

  • 2022-12-23
  • 2021-10-05
  • 2022-12-23
  • 2021-08-30
  • 2021-07-08
  • 2021-11-02
  • 2021-09-24
猜你喜欢
  • 2022-01-13
  • 2021-04-23
  • 2021-08-12
  • 2022-12-23
  • 2022-01-08
  • 2021-06-27
  • 2022-12-23
相关资源
相似解决方案