Default parameter for CancellationToken

前端 未结 6 2072
猫巷女王i
猫巷女王i 2021-02-06 20:05

I have some async code that I would like to add a CancellationToken to. However, there are many implementations where this is not needed so I would like to have a d

6条回答
  •  太阳男子
    2021-02-06 20:27

    It turns out that the following works:

    Task DoStuff(...., CancellationToken ct = default(CancellationToken))
    

    ...or:

    Task DoStuff(...., CancellationToken ct = default) // C# 7.1 and later
    

    which, according to the documentation, is interpreted the same as CancellationToken.None:

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

提交回复
热议问题