Default parameter for CancellationToken

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

    Here are several solutions, in descending order of general goodness:

    1. Using default(CancellationToken) as default value:

    Task DoAsync(CancellationToken ct = default(CancellationToken)) { … }
    

    Semantically, CancellationToken.None would be the ideal candidate for the default, but cannot be used as such because it isn't a compile-time constant. default(CancellationToken) is the next best thing because it is a compile-time constant and officially documented to be equivalent to CancellationToken.None.

    2. Providing a method overload without a CancellationToken parameter:

    Or, if you prefer method overloads over optional parameters (see this and this question on that topic):

    Task DoAsync(CancellationToken ct) { … } // actual method always requires a token
    Task DoAsync() => DoAsync(CancellationToken.None); // overload producing a default token
    

    For interface methods, the same can be achieved using extension methods:

    interface IFoo
    {
        Task DoAsync(CancellationToken ct);
    }
    
    static class Foo
    {
        public static Task DoAsync(this IFoo foo) => foo.DoAsync(CancellationToken.None);
    }
    

    This results in a slimmer interface and spares implementers from explicitly writing the forwarding method overload.

    3. Making the parameter nullable and using null as default value:

    Task DoAsync(…, CancellationToken? ct = null)
    {
        … ct ?? CancellationToken.None …
    }
    

    I like this solution least because nullable types come with a small runtime overhead, and references to the cancellation token become more verbose because of the null coalescing operator ??.

提交回复
热议问题