diff --git a/docs/csharp/programming-guide/classes-and-structs/local-functions.md b/docs/csharp/programming-guide/classes-and-structs/local-functions.md index 0944d4471c849..8db242c8cdf76 100644 --- a/docs/csharp/programming-guide/classes-and-structs/local-functions.md +++ b/docs/csharp/programming-guide/classes-and-structs/local-functions.md @@ -66,16 +66,6 @@ If you put iterator logic into a local function, argument validation exceptions :::code language="csharp" source="snippets/local-functions/IteratorWithLocal.cs" ::: -You can use local functions in a similar way with asynchronous operations. Exceptions thrown in an async method surface when the corresponding task is awaited. Local functions allow your code to fail fast and allow your exception to be both thrown and observed synchronously. - -The following example uses an asynchronous method named `GetMultipleAsync` to pause for a specified number of seconds and return a value that is a random multiple of that number of seconds. The maximum delay is 5 seconds; an results if the value is greater than 5. As the following example shows, the exception that is thrown when a value of 6 is passed to the `GetMultipleAsync` method is observed only when the task is awaited. - -:::code language="csharp" source="snippets/local-functions/AsyncWithoutLocal.cs" ::: - -Like with the method iterator, you can refactor the preceding example and put the code of asynchronous operation in a local function. As the output from the following example shows, the is thrown as soon as the `GetMultiple` method is called. - -:::code language="csharp" source="snippets/local-functions/AsyncWithLocal.cs" ::: - ## Local functions vs. lambda expressions At first glance, local functions and [lambda expressions](../../language-reference/operators/lambda-expressions.md) are very similar. In many cases, the choice between using lambda expressions and local functions is a matter of style and personal preference. However, there are real differences in where you can use one or the other that you should be aware of. diff --git a/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/AsyncWithLocal.cs b/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/AsyncWithLocal.cs deleted file mode 100644 index 5a473fbe0ae53..0000000000000 --- a/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/AsyncWithLocal.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Threading.Tasks; - -public class AsyncWithLocalExample -{ - public static async Task Main() - { - var t = GetMultiple(6); // line 8 - Console.WriteLine("Got the task"); - - var result = await t; - Console.WriteLine($"The returned value is {result:N0}"); - } - - static Task GetMultiple(int delayInSeconds) - { - if (delayInSeconds < 0 || delayInSeconds > 5) - throw new ArgumentOutOfRangeException(nameof(delayInSeconds), "Delay cannot exceed 5 seconds."); - - return GetValueAsync(); - - async Task GetValueAsync() - { - await Task.Delay(delayInSeconds * 1000); - return delayInSeconds * new Random().Next(2,10); - } - } -} -// The example displays the output like this: -// -// Unhandled exception. System.ArgumentOutOfRangeException: Delay cannot exceed 5 seconds. (Parameter 'delayInSeconds') -// at AsyncWithLocalExample.GetMultiple(Int32 delayInSeconds) in AsyncWithLocal.cs:line 18 -// at AsyncWithLocalExample.Main() in AsyncWithLocal.cs:line 8 diff --git a/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/AsyncWithoutLocal.cs b/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/AsyncWithoutLocal.cs deleted file mode 100644 index 4188c9b36414e..0000000000000 --- a/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/AsyncWithoutLocal.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Threading.Tasks; - -public class AsyncWithoutLocalExample -{ - public static async Task Main() - { - var t = GetMultipleAsync(6); - Console.WriteLine("Got the task"); - - var result = await t; // line 11 - Console.WriteLine($"The returned value is {result:N0}"); - } - - static async Task GetMultipleAsync(int delayInSeconds) - { - if (delayInSeconds < 0 || delayInSeconds > 5) - throw new ArgumentOutOfRangeException(nameof(delayInSeconds), "Delay cannot exceed 5 seconds."); - - await Task.Delay(delayInSeconds * 1000); - return delayInSeconds * new Random().Next(2,10); - } -} -// The example displays the output like this: -// -// Got the task -// Unhandled exception. System.ArgumentOutOfRangeException: Delay cannot exceed 5 seconds. (Parameter 'delayInSeconds') -// at AsyncWithoutLocalExample.GetMultipleAsync(Int32 delayInSeconds) in AsyncWithoutLocal.cs:line 18 -// at AsyncWithoutLocalExample.Main() in AsyncWithoutLocal.cs:line 11 diff --git a/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/Program.cs b/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/Program.cs index 875b2ef4b0605..f03dd15ad8672 100644 --- a/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/Program.cs +++ b/docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/Program.cs @@ -71,7 +71,7 @@ public static int LambdaFactorial(int n) // // - public Task PerformLongRunningWorkLambda(string address, int index, string name) + public async Task PerformLongRunningWorkLambda(string address, int index, string name) { if (string.IsNullOrWhiteSpace(address)) throw new ArgumentException(message: "An address is required", paramName: nameof(address)); @@ -87,12 +87,12 @@ public Task PerformLongRunningWorkLambda(string address, int index, stri return $"The results are {interimResult} and {secondResult}. Enjoy."; }; - return longRunningWorkImplementation(); + return await longRunningWorkImplementation(); } // // - public Task PerformLongRunningWork(string address, int index, string name) + public async Task PerformLongRunningWork(string address, int index, string name) { if (string.IsNullOrWhiteSpace(address)) throw new ArgumentException(message: "An address is required", paramName: nameof(address)); @@ -101,7 +101,7 @@ public Task PerformLongRunningWork(string address, int index, string nam if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException(message: "You must supply a name", paramName: nameof(name)); - return longRunningWorkImplementation(); + return await longRunningWorkImplementation(); async Task longRunningWorkImplementation() {