Skip to content

Provide common Task results for "true", "false", "empty", "null", etc #22627

@davkean

Description

@davkean

It's extremely common both in the framework itself and applications built on top of the framework to statically cache your own versions of Task with common results retrieved via Task.FromResult, similar to:

    /// <summary>
    ///     Provides sentinel Tasks that represent commonly returned values.
    /// </summary>
    internal class TaskResult
    {
        /// <summary>
        ///     Represents a Task that's completed successfully with the result of <see langword="false"/>.
        /// </summary>
        public static readonly Task<bool> False = Task.FromResult(false);

        /// <summary>
        ///     Represents a Task that's completed successfully with the result of <see langword="true"/>.
        /// </summary>
        public static readonly Task<bool> True = Task.FromResult(true);
    }

Just a few examples in 5 minutes of searching, are:

AsyncTaskCache in mscorlib
ADP in System.Data
AsyncHelper in System.Xml
TplExtensions in Microsoft.VisualStudio.Threading
TaskResult in ProjectSystem

Similar to what it did with Task.CompletedTask, the BCL should just provide these already cached values for common values, including true, false, empty string ("") and null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions