Default parameter for CancellationToken

前端 未结 6 2092
猫巷女王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:37

    Tofutim's answer is one way, but from the comments I see that people have issues with it.

    In that case, I did suggest that one can have a method as follows:

    Task DoStuff(...., CancellationToken ct)
    {
    } 
    

    and overload it as:

    Task DoStuff(....)
    {
        return DoStuff(...., CancellationToken.None);
    }
    

    This compiles, because the value of CancellationToken.None is not required at compile time.

提交回复
热议问题