-
Notifications
You must be signed in to change notification settings - Fork 6k
Add async stream return types #17843
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
Merged
BillWagner
merged 7 commits into
dotnet:master
from
BillWagner:add-async-stream-return-types
Apr 14, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f4941d
Add IAsyncEnumerable
BillWagner 7ac1d50
grammar cleanup
BillWagner e56c791
move sample files
BillWagner 6fd560e
this commit migrates the samples
BillWagner b7bec31
fix typo
BillWagner e7f0e91
Apply suggestions from code review
BillWagner 9bc8eee
respond to additional feedback.
BillWagner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
docs/csharp/programming-guide/concepts/async/snippets/AsyncExamples.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<Nullable>Enable</Nullable> | ||
<StartupObject>AsyncExamples.Program</StartupObject> | ||
</PropertyGroup> | ||
|
||
</Project> |
42 changes: 42 additions & 0 deletions
42
docs/csharp/programming-guide/concepts/async/snippets/AsyncStreams.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace AsyncExamples | ||
{ | ||
|
||
public static class AsyncStreamExample | ||
{ | ||
|
||
public static async Task Examples() | ||
{ | ||
await foreach (var word in ReadWordsFromStream()) | ||
Console.WriteLine(word); | ||
} | ||
|
||
// <SnippetGenerateAsyncStream> | ||
private static async IAsyncEnumerable<string> ReadWordsFromStream() | ||
{ | ||
string data = | ||
@"This is a line of text. | ||
Here is the second line of text. | ||
And there is one more for good measure. | ||
Wait, that was the penultimate line."; | ||
|
||
using var readStream = new StringReader(data); | ||
|
||
string? line = await readStream.ReadLineAsync(); | ||
while (line != null) | ||
{ | ||
var words = line.Split(' ',StringSplitOptions.RemoveEmptyEntries); | ||
foreach (var word in words) | ||
{ | ||
yield return word; | ||
} | ||
line = await readStream.ReadLineAsync(); | ||
} | ||
} | ||
// </SnippetGenerateAsyncStream> | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
docs/csharp/programming-guide/concepts/async/snippets/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace AsyncExamples | ||
{ | ||
public static class Program | ||
{ | ||
static async Task Main() | ||
{ | ||
await FirstExample.ShowTodaysInfo(); | ||
await SecondExample.ShowTodaysInfo(); | ||
await ExampleTask.DisplayCurrentInfo(); | ||
await AwaitTaskExample.DisplayCurrentInfo(); | ||
await AsyncVoidExample.Main(); | ||
await AsyncStreamExample.Examples(); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
docs/csharp/programming-guide/concepts/async/snippets/async-returns1.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
public class FirstExample | ||
{ | ||
// <SnippetFirstExample> | ||
public static async Task<string> ShowTodaysInfo() | ||
{ | ||
string ret = $"Today is {DateTime.Today:D}\n" + | ||
"Today's hours of leisure: " + | ||
$"{await GetLeisureHours()}"; | ||
return ret; | ||
} | ||
|
||
static async Task<int> GetLeisureHours() | ||
{ | ||
// Task.FromResult is a placeholder for actual work that returns a string. | ||
var today = await Task.FromResult<string>(DateTime.Now.DayOfWeek.ToString()); | ||
|
||
// The method then can process the result in some way. | ||
int leisureHours; | ||
if (today.First() == 'S') | ||
leisureHours = 16; | ||
else | ||
leisureHours = 5; | ||
|
||
return leisureHours; | ||
} | ||
// The example displays output like the following: | ||
// Today is Wednesday, May 24, 2017 | ||
// Today's hours of leisure: 5 | ||
// </SnippetFirstExample> | ||
} |
34 changes: 34 additions & 0 deletions
34
docs/csharp/programming-guide/concepts/async/snippets/async-returns1a.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
public class SecondExample | ||
{ | ||
public static async Task<string> ShowTodaysInfo() | ||
{ | ||
// <SnippetSecondVersion> | ||
var integerTask = GetLeisureHours(); | ||
|
||
// You can do other work that does not rely on integerTask before awaiting. | ||
string ret = $"Today is {DateTime.Today:D}\n" + | ||
"Today's hours of leisure: " + | ||
$"{await integerTask}"; | ||
return ret; | ||
// </SnippetSecondVersion> | ||
} | ||
|
||
static async Task<int> GetLeisureHours() | ||
{ | ||
// Task.FromResult is a placeholder for actual work that returns a string. | ||
var today = await Task.FromResult<string>(DateTime.Now.DayOfWeek.ToString()); | ||
|
||
// The method then can process the result in some way. | ||
int leisureHours; | ||
if (today.First() == 'S') | ||
leisureHours = 16; | ||
else | ||
leisureHours = 5; | ||
|
||
return leisureHours; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
docs/csharp/programming-guide/concepts/async/snippets/async-returns2.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
public class ExampleTask | ||
{ | ||
// <SnippetTaskReturn> | ||
public static async Task DisplayCurrentInfo() | ||
{ | ||
await WaitAndApologize(); | ||
Console.WriteLine($"Today is {DateTime.Now:D}"); | ||
Console.WriteLine($"The current time is {DateTime.Now.TimeOfDay:t}"); | ||
Console.WriteLine("The current temperature is 76 degrees."); | ||
} | ||
|
||
static async Task WaitAndApologize() | ||
{ | ||
// Task.Delay is a placeholder for actual work. | ||
await Task.Delay(2000); | ||
// Task.Delay delays the following line by two seconds. | ||
Console.WriteLine("\nSorry for the delay. . . .\n"); | ||
} | ||
// The example displays the following output: | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Sorry for the delay. . . . | ||
// | ||
// Today is Wednesday, May 24, 2017 | ||
// The current time is 15:25:16.2935649 | ||
// The current temperature is 76 degrees. | ||
// </SnippetTaskReturn> | ||
} |
32 changes: 32 additions & 0 deletions
32
docs/csharp/programming-guide/concepts/async/snippets/async-returns2a.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
public class AwaitTaskExample | ||
{ | ||
public static async Task DisplayCurrentInfo() | ||
{ | ||
// <SnippetAwaitTask> | ||
Task wait = WaitAndApologize(); | ||
|
||
string output = $"Today is {DateTime.Now:D}\n" + | ||
$"The current time is {DateTime.Now.TimeOfDay:t}\n" + | ||
$"The current temperature is 76 degrees.\n"; | ||
await wait; | ||
Console.WriteLine(output); | ||
// </SnippetAwaitTask> | ||
} | ||
|
||
static async Task WaitAndApologize() | ||
{ | ||
// Task.Delay is a placeholder for actual work. | ||
await Task.Delay(2000); | ||
// Task.Delay delays the following line by two seconds. | ||
Console.WriteLine("\nSorry for the delay. . . .\n"); | ||
} | ||
} | ||
// The example displays the following output: | ||
// Sorry for the delay. . . . | ||
// | ||
// Today is Wednesday, May 24, 2017 | ||
// The current time is 15:25:16.2935649 | ||
// The current temperature is 76 degrees. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
class Program | ||
{ | ||
static Random rnd; | ||
static Random? rnd; | ||
|
||
static void Main() | ||
{ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.