Default parameter for CancellationToken

前端 未结 6 2217
猫巷女王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条回答
  •  萌比男神i
    2021-02-06 20:29

    Another option is to use a Nullable parameter, default it to null, and deal with it inside the method:

    Task DoStuff(...., CancellationToken? ct = null) {
        var token = ct ?? CancellationToken.None;
        ...
    }
    

提交回复
热议问题