Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 4 additions & 25 deletions snippets/csharp/new-in-7/AsyncWork.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace new_in_7
{
public class AsyncWork
{
#region 29_TaskExample
// <SnippetTaskExample>
public Task<string> PerformLongRunningWork(string address, int index, string name)
{
if (string.IsNullOrWhiteSpace(address))
Expand All @@ -27,7 +24,7 @@ async Task<string> longRunningWorkImplementation()
return $"The results are {interimResult} and {secondResult}. Enjoy.";
}
}
#endregion
// </SnippetTaskExample>

private async Task<string> SecondStep(Int32 index, String name)
{
Expand Down Expand Up @@ -62,30 +59,12 @@ public Task<string> PerformLongRunningWorkLambda(string address, int index, stri
}
#endregion

#region 30_UsingValueTask
// <SnippetUsingValueTask>
public async ValueTask<int> Func()
{
await Task.Delay(100);
return 5;
}
#endregion

#region 31_AsyncOptimizedValueTask
public ValueTask<int> CachedFunc()
{
return (cache) ? new ValueTask<int>(cacheResult) : new ValueTask<int>(LoadCache());
}
private bool cache = false;
private int cacheResult;
private async Task<int> LoadCache()
{
// simulate async work:
await Task.Delay(100);
cacheResult = 100;
cache = true;
return cacheResult;
}
#endregion

// </SnippetUsingValueTask>
}
}
101 changes: 70 additions & 31 deletions snippets/csharp/new-in-7/Iterator.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace new_in_7
{
public static class Iterator
{
#region 25_IteratorMethod
public static IEnumerable<char> AlphabetSubset(char start, char end)
public static void IteratorTest()
{
if (start < 'a' || start > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
if (end < 'a' || end > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");
// <SnippetIteratorMethod>
IEnumerable<char> AlphabetSubset(char start, char end)
{
if (start < 'a' || start > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
if (end < 'a' || end > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");

if (end <= start)
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
for (var c = start; c < end; c++)
yield return c;
if (end <= start)
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
for (var c = start; c < end; c++)
yield return c;
}


try
{
var resultSet1 = AlphabetSubset('d', 'r');
var resultSet2 = AlphabetSubset('f', 'a');
Console.WriteLine("iterators created");
foreach (var thing1 in resultSet1)
Console.Write($"{thing1}, ");
Console.WriteLine();
foreach (var thing2 in resultSet2)
Console.Write($"{thing2}, ");
Console.WriteLine();
}
catch (ArgumentException)
{
Console.WriteLine("Caught an argument exception");
}
// </SnippetIteratorMethod>
}
#endregion

#region 27_IteratorMethodRefactored
public static IEnumerable<char> AlphabetSubset2(char start, char end)
public static void IteratorTestLocal()
{
if (start < 'a' || start > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
if (end < 'a' || end > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");
// <SnippetIteratorMethodLocalInteractive>
IEnumerable<char> AlphabetSubset(char start, char end)
{
if (start < 'a' || start > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
if (end < 'a' || end > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");

if (end <= start)
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
return alphabetSubsetImplementation(start, end);
}
if (end <= start)
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");

private static IEnumerable<char> alphabetSubsetImplementation(char start, char end)
{
for (var c = start; c < end; c++)
yield return c;
return alphabetSubsetImplementation();

IEnumerable<char> alphabetSubsetImplementation()
{
for (var c = start; c < end; c++)
yield return c;
}
}

try
{
var resultSet1 = AlphabetSubset('d', 'r');
var resultSet2 = AlphabetSubset('f', 'a');
Console.WriteLine("iterators created");
foreach (var thing1 in resultSet1)
Console.Write($"{thing1}, ");
Console.WriteLine();
foreach (var thing2 in resultSet2)
Console.Write($"{thing2}, ");
Console.WriteLine();
}
catch (ArgumentException)
{
Console.WriteLine("Caught an argument exception");
}
// </SnippetIteratorMethodLocalInteractive>
}
#endregion

#region 28_IteratorMethodLocal
// <SnippetIteratorMethodLocal>
public static IEnumerable<char> AlphabetSubset3(char start, char end)
{
if (start < 'a' || start > 'z')
Expand All @@ -62,7 +101,7 @@ IEnumerable<char> alphabetSubsetImplementation()
yield return c;
}
}
#endregion
// </SnippetIteratorMethodLocal>
}

}
20 changes: 0 additions & 20 deletions snippets/csharp/new-in-7/MathUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,4 @@ public static int LambdaFactorial(int n)
#endregion
}

public class FactorialExample
{
#region 38_LocalFunctionFactorial
public int LocalFunctionFactorial(int n)
{
recursiveCalls = 0;
return nthFactorial(n);

int nthFactorial(int number)
{
recursiveCalls++;
Console.WriteLine($"We've made {recursiveCalls} calls to this method");
return (number < 2) ?
1 : number * nthFactorial(number - 1);
}
}
#endregion

private int recursiveCalls = 0;
}
}
66 changes: 51 additions & 15 deletions snippets/csharp/new-in-7/MatrixSearch.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace new_in_7
{
static class MatrixSearch
{
#region 20_FindReturningIndices
public static (int i, int j) Find(int[,] matrix, Func<int, bool> predicate)
public static void EverythingByValue()
{
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
if (predicate(matrix[i, j]))
return (i, j);
return (-1, -1); // Not found
// <SnippetEverythingByValue>
(int i, int j) Find(int[,] matrix, Func<int, bool> predicate)
{
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
if (predicate(matrix[i, j]))
return (i, j);
return (-1, -1); // Not found
}
// </SnippetEverythingByValue>

// <SnippetTestByValue>
int[,] sourceMatrix = new int[10, 10];
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
sourceMatrix[x, y] = x * 10 + y;

var indices = Find(sourceMatrix, (val) => val == 42);
Console.WriteLine(indices);
sourceMatrix[indices.i, indices.j] = 24;
// </SnippetTestByValue>
}

public static void EverythingByRef()
{
// <SnippetEverythingByRef>
ref int Find(int[,] matrix, Func<int, bool> predicate)
{
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
if (predicate(matrix[i, j]))
return ref matrix[i, j];
throw new InvalidOperationException("Not found");
}
// </SnippetEverythingByRef>

// <SnippetTestByRef>
int[,] sourceMatrix = new int[10, 10];
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
sourceMatrix[x, y] = x * 10 + y;

ref var item = ref Find(sourceMatrix, (val) => val == 42);
Console.WriteLine(item);
item = 24;
Console.WriteLine(sourceMatrix[4, 2]);
// </SnippetTestByRef>
}
#endregion

#region 22_FindReturningRef
public static ref int Find3(int[,] matrix, Func<int, bool> predicate)
// <SnippetFindReturningRef>
public static ref int Find(int[,] matrix, Func<int, bool> predicate)
{
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
if (predicate(matrix[i, j]))
return ref matrix[i, j];
throw new InvalidOperationException("Not found");
}
#endregion
// </SnippetFindReturningRef>
}
}
28 changes: 8 additions & 20 deletions snippets/csharp/new-in-7/Point.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace new_in_7
namespace new_in_7
{
#region 11_PointWithDeconstruction
// <SnippetPointWithDeconstruction>
public class Point
{
public Point(double x, double y)
{
this.X = x;
this.Y = y;
}

public Point(double x, double y)
=> (X, Y) = (x, y);

public double X { get; }
public double Y { get; }

public void Deconstruct(out double x, out double y)
{
x = this.X;
y = this.Y;
}
public void Deconstruct(out double x, out double y) =>
(x, y) = (X, Y);
}
#endregion
// </SnippetPointWithDeconstruction>
}
Loading