Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taskextensions sample notebook #199

Merged
merged 7 commits into from
Oct 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Corvus.Extensions.Samples/TaskExtensionsSample.dib
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!markdown

## TaskExtensions - Sample notebook

jongeorge1 marked this conversation as resolved.
Show resolved Hide resolved
#!markdown

First, add a reference to the `Corvus.Extensions` NuGet package.

#!csharp

#r "nuget: Corvus.Extensions, 1.1.4"

#!markdown

Add using statement for `Corvus.Extensions` namespace

#!csharp

using Corvus.Extensions;
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;

#!markdown

## Examples

#!markdown

### `CastWithConversion()`

Checks to see if a `Task` actually has a result and converts to a `Task` of the relevant type.

#!csharp

class Base
{

}

class Derived : Base
{

}

#!markdown

The following cell returns an error, as `Task<T>` is not covariant.

#!csharp

Task t = Task.FromResult(new Derived());

Task<Base> tb = (Task<Base>)t;
tb.Result

#!markdown

Using `CastWithConversion<>()` allows us to cast the `Task` to a `Task<Base>`.

#!csharp

Task<Base> tb = t.CastWithConversion<Base>();
tb.Result