Task.FromResult<'TResult> Method ('TResult)

.NET Framework (current version)
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates a Task<'TResult> that's completed successfully with the specified result.

Namespace:   System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

static member FromResult<'TResult> : 
        result:'TResult -> Task<'TResult>

Parameters

result
Type: TResult

The result to store into the completed task.

Return Value

Type: System.Threading.Tasks.Task<'TResult>

The successfully completed task.

Type Parameters

TResult

The type of the result returned by the task.

This method creates a Task<'TResult> object whose Task<'TResult>.Result property is result and whose Status property is TaskStatus.RanToCompletion. The method is commonly used when the return value of a task is immediately known without executing a longer code path. The example provides an illustration.

To create a Task object that does not return a value, retrieve the Task object from the CompletedTask property.

The following example is a command-line utility that calculates the number of bytes in the files in each directory whose name is passed as a command-line argument. Rather than executing a longer code path that instantiates a FileStream object and retrieves the value of its FileStream.Length property for each file in the directory, the example simply calls the FromResult<'TResult> method to create a task whose Task<'TResult>.Result property is zero (0) if a directory has no files.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: