Skip to content

Commit f092292

Browse files
authored
Updates for interactive in C# (#754)
Many of the samples changed. This sample now supports "What's new in C# 7", and the exploratory tutorial for C# 7. Required for dotnet/docs#11442
1 parent 1072e7a commit f092292

File tree

10 files changed

+265
-357
lines changed

10 files changed

+265
-357
lines changed

snippets/csharp/new-in-7/AsyncWork.cs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using System.Threading.Tasks;
63

74
namespace new_in_7
85
{
96
public class AsyncWork
107
{
11-
#region 29_TaskExample
8+
// <SnippetTaskExample>
129
public Task<string> PerformLongRunningWork(string address, int index, string name)
1310
{
1411
if (string.IsNullOrWhiteSpace(address))
@@ -27,7 +24,7 @@ async Task<string> longRunningWorkImplementation()
2724
return $"The results are {interimResult} and {secondResult}. Enjoy.";
2825
}
2926
}
30-
#endregion
27+
// </SnippetTaskExample>
3128

3229
private async Task<string> SecondStep(Int32 index, String name)
3330
{
@@ -62,30 +59,12 @@ public Task<string> PerformLongRunningWorkLambda(string address, int index, stri
6259
}
6360
#endregion
6461

65-
#region 30_UsingValueTask
62+
// <SnippetUsingValueTask>
6663
public async ValueTask<int> Func()
6764
{
6865
await Task.Delay(100);
6966
return 5;
7067
}
71-
#endregion
72-
73-
#region 31_AsyncOptimizedValueTask
74-
public ValueTask<int> CachedFunc()
75-
{
76-
return (cache) ? new ValueTask<int>(cacheResult) : new ValueTask<int>(LoadCache());
77-
}
78-
private bool cache = false;
79-
private int cacheResult;
80-
private async Task<int> LoadCache()
81-
{
82-
// simulate async work:
83-
await Task.Delay(100);
84-
cacheResult = 100;
85-
cache = true;
86-
return cacheResult;
87-
}
88-
#endregion
89-
68+
// </SnippetUsingValueTask>
9069
}
9170
}
Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,88 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63

74
namespace new_in_7
85
{
96
public static class Iterator
107
{
11-
#region 25_IteratorMethod
12-
public static IEnumerable<char> AlphabetSubset(char start, char end)
8+
public static void IteratorTest()
139
{
14-
if (start < 'a' || start > 'z')
15-
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
16-
if (end < 'a' || end > 'z')
17-
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");
10+
// <SnippetIteratorMethod>
11+
IEnumerable<char> AlphabetSubset(char start, char end)
12+
{
13+
if (start < 'a' || start > 'z')
14+
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
15+
if (end < 'a' || end > 'z')
16+
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");
1817

19-
if (end <= start)
20-
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
21-
for (var c = start; c < end; c++)
22-
yield return c;
18+
if (end <= start)
19+
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
20+
for (var c = start; c < end; c++)
21+
yield return c;
22+
}
23+
24+
25+
try
26+
{
27+
var resultSet1 = AlphabetSubset('d', 'r');
28+
var resultSet2 = AlphabetSubset('f', 'a');
29+
Console.WriteLine("iterators created");
30+
foreach (var thing1 in resultSet1)
31+
Console.Write($"{thing1}, ");
32+
Console.WriteLine();
33+
foreach (var thing2 in resultSet2)
34+
Console.Write($"{thing2}, ");
35+
Console.WriteLine();
36+
}
37+
catch (ArgumentException)
38+
{
39+
Console.WriteLine("Caught an argument exception");
40+
}
41+
// </SnippetIteratorMethod>
2342
}
24-
#endregion
2543

26-
#region 27_IteratorMethodRefactored
27-
public static IEnumerable<char> AlphabetSubset2(char start, char end)
44+
public static void IteratorTestLocal()
2845
{
29-
if (start < 'a' || start > 'z')
30-
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
31-
if (end < 'a' || end > 'z')
32-
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");
46+
// <SnippetIteratorMethodLocalInteractive>
47+
IEnumerable<char> AlphabetSubset(char start, char end)
48+
{
49+
if (start < 'a' || start > 'z')
50+
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
51+
if (end < 'a' || end > 'z')
52+
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");
3353

34-
if (end <= start)
35-
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
36-
return alphabetSubsetImplementation(start, end);
37-
}
54+
if (end <= start)
55+
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
3856

39-
private static IEnumerable<char> alphabetSubsetImplementation(char start, char end)
40-
{
41-
for (var c = start; c < end; c++)
42-
yield return c;
57+
return alphabetSubsetImplementation();
58+
59+
IEnumerable<char> alphabetSubsetImplementation()
60+
{
61+
for (var c = start; c < end; c++)
62+
yield return c;
63+
}
64+
}
65+
66+
try
67+
{
68+
var resultSet1 = AlphabetSubset('d', 'r');
69+
var resultSet2 = AlphabetSubset('f', 'a');
70+
Console.WriteLine("iterators created");
71+
foreach (var thing1 in resultSet1)
72+
Console.Write($"{thing1}, ");
73+
Console.WriteLine();
74+
foreach (var thing2 in resultSet2)
75+
Console.Write($"{thing2}, ");
76+
Console.WriteLine();
77+
}
78+
catch (ArgumentException)
79+
{
80+
Console.WriteLine("Caught an argument exception");
81+
}
82+
// </SnippetIteratorMethodLocalInteractive>
4383
}
44-
#endregion
4584

46-
#region 28_IteratorMethodLocal
85+
// <SnippetIteratorMethodLocal>
4786
public static IEnumerable<char> AlphabetSubset3(char start, char end)
4887
{
4988
if (start < 'a' || start > 'z')
@@ -62,7 +101,7 @@ IEnumerable<char> alphabetSubsetImplementation()
62101
yield return c;
63102
}
64103
}
65-
#endregion
104+
// </SnippetIteratorMethodLocal>
66105
}
67106

68107
}

snippets/csharp/new-in-7/MathUtilities.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,4 @@ public static int LambdaFactorial(int n)
3131
#endregion
3232
}
3333

34-
public class FactorialExample
35-
{
36-
#region 38_LocalFunctionFactorial
37-
public int LocalFunctionFactorial(int n)
38-
{
39-
recursiveCalls = 0;
40-
return nthFactorial(n);
41-
42-
int nthFactorial(int number)
43-
{
44-
recursiveCalls++;
45-
Console.WriteLine($"We've made {recursiveCalls} calls to this method");
46-
return (number < 2) ?
47-
1 : number * nthFactorial(number - 1);
48-
}
49-
}
50-
#endregion
51-
52-
private int recursiveCalls = 0;
53-
}
5434
}
Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,69 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace new_in_7
84
{
95
static class MatrixSearch
106
{
11-
#region 20_FindReturningIndices
12-
public static (int i, int j) Find(int[,] matrix, Func<int, bool> predicate)
7+
public static void EverythingByValue()
138
{
14-
for (int i = 0; i < matrix.GetLength(0); i++)
15-
for (int j = 0; j < matrix.GetLength(1); j++)
16-
if (predicate(matrix[i, j]))
17-
return (i, j);
18-
return (-1, -1); // Not found
9+
// <SnippetEverythingByValue>
10+
(int i, int j) Find(int[,] matrix, Func<int, bool> predicate)
11+
{
12+
for (int i = 0; i < matrix.GetLength(0); i++)
13+
for (int j = 0; j < matrix.GetLength(1); j++)
14+
if (predicate(matrix[i, j]))
15+
return (i, j);
16+
return (-1, -1); // Not found
17+
}
18+
// </SnippetEverythingByValue>
19+
20+
// <SnippetTestByValue>
21+
int[,] sourceMatrix = new int[10, 10];
22+
for (int x = 0; x < 10; x++)
23+
for (int y = 0; y < 10; y++)
24+
sourceMatrix[x, y] = x * 10 + y;
25+
26+
var indices = Find(sourceMatrix, (val) => val == 42);
27+
Console.WriteLine(indices);
28+
sourceMatrix[indices.i, indices.j] = 24;
29+
// </SnippetTestByValue>
30+
}
31+
32+
public static void EverythingByRef()
33+
{
34+
// <SnippetEverythingByRef>
35+
ref int Find(int[,] matrix, Func<int, bool> predicate)
36+
{
37+
for (int i = 0; i < matrix.GetLength(0); i++)
38+
for (int j = 0; j < matrix.GetLength(1); j++)
39+
if (predicate(matrix[i, j]))
40+
return ref matrix[i, j];
41+
throw new InvalidOperationException("Not found");
42+
}
43+
// </SnippetEverythingByRef>
44+
45+
// <SnippetTestByRef>
46+
int[,] sourceMatrix = new int[10, 10];
47+
for (int x = 0; x < 10; x++)
48+
for (int y = 0; y < 10; y++)
49+
sourceMatrix[x, y] = x * 10 + y;
50+
51+
ref var item = ref Find(sourceMatrix, (val) => val == 42);
52+
Console.WriteLine(item);
53+
item = 24;
54+
Console.WriteLine(sourceMatrix[4, 2]);
55+
// </SnippetTestByRef>
1956
}
20-
#endregion
2157

22-
#region 22_FindReturningRef
23-
public static ref int Find3(int[,] matrix, Func<int, bool> predicate)
58+
// <SnippetFindReturningRef>
59+
public static ref int Find(int[,] matrix, Func<int, bool> predicate)
2460
{
2561
for (int i = 0; i < matrix.GetLength(0); i++)
2662
for (int j = 0; j < matrix.GetLength(1); j++)
2763
if (predicate(matrix[i, j]))
2864
return ref matrix[i, j];
2965
throw new InvalidOperationException("Not found");
3066
}
31-
#endregion
67+
// </SnippetFindReturningRef>
3268
}
3369
}

snippets/csharp/new-in-7/Point.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace new_in_7
1+
namespace new_in_7
82
{
9-
#region 11_PointWithDeconstruction
3+
// <SnippetPointWithDeconstruction>
104
public class Point
115
{
12-
public Point(double x, double y)
13-
{
14-
this.X = x;
15-
this.Y = y;
16-
}
17-
6+
public Point(double x, double y)
7+
=> (X, Y) = (x, y);
8+
189
public double X { get; }
1910
public double Y { get; }
2011

21-
public void Deconstruct(out double x, out double y)
22-
{
23-
x = this.X;
24-
y = this.Y;
25-
}
12+
public void Deconstruct(out double x, out double y) =>
13+
(x, y) = (X, Y);
2614
}
27-
#endregion
15+
// </SnippetPointWithDeconstruction>
2816
}

0 commit comments

Comments
 (0)