Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 0f8cff2

Browse files
authored
Create C# 7 exploration (#5)
* checkpoint. Roughly half way done updating this exploration. * test for CI build * Getting more of the tutorials ready. * final review * remove debugging statement in build
1 parent da1952b commit 0f8cff2

26 files changed

+1090
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# User-specific files (MonoDevelop/Xamarin Studio)
1414
*.userprefs
1515

16+
# VS Code folder:
17+
**/.vscode/
18+
1619
# Mono auto generated files
1720
mono_crash.*
1821

@@ -350,3 +353,4 @@ MigrationBackup/
350353

351354
# binlog files for dotnet-try
352355
**/*.binlog
356+
NuGet.config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
<LangVersion>8.0</LangVersion>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.2.0-alpha.19174.3" />
12+
</ItemGroup>
13+
14+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ExploreCsharpSeven
5+
{
6+
public static class GenericConstraints
7+
{
8+
#region DeclareEnumConstraint
9+
public static Dictionary<int, string> EnumNamedValues<T>() where T : System.Enum
10+
{
11+
var result = new Dictionary<int, string>();
12+
var values = Enum.GetValues(typeof(T));
13+
14+
foreach (int item in values)
15+
result.Add(item, Enum.GetName(typeof(T), item));
16+
return result;
17+
}
18+
#endregion
19+
20+
#region DeclareEnum
21+
enum Rainbow
22+
{
23+
Red,
24+
Orange,
25+
Yellow,
26+
Green,
27+
Blue,
28+
Indigo,
29+
Violet
30+
}
31+
#endregion
32+
33+
public static int TestEnumNamedValues()
34+
{
35+
#region TestMapEnumValues
36+
var map = EnumNamedValues<Rainbow>();
37+
38+
foreach (var pair in map)
39+
Console.WriteLine($"{pair.Key}:\t{pair.Value}");
40+
#endregion
41+
return 0;
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ExploreCsharpSeven
6+
{
7+
static class GenericPatterns
8+
{
9+
#region GenericSwitchTypePattern
10+
public static void TestType(object obj)
11+
{
12+
switch (obj)
13+
{
14+
case 5:
15+
Console.WriteLine("The object is 5");
16+
break;
17+
case int i:
18+
Console.WriteLine($"The object is an integer: {i}");
19+
break;
20+
case null:
21+
Console.WriteLine($"The object is null");
22+
break;
23+
case long l:
24+
Console.WriteLine($"The object is a long: {l}");
25+
break;
26+
case double d:
27+
Console.WriteLine($"The object is a double: {d}");
28+
break;
29+
case string s when s.StartsWith("This"):
30+
Console.WriteLine($"This was a string that started with the word 'This': {s}");
31+
break;
32+
case string s when s.StartsWith("This"):
33+
Console.WriteLine($"This was a string that started with the word 'This': {s}");
34+
break;
35+
case string s:
36+
Console.WriteLine($"The object is a string: {s}");
37+
break;
38+
default:
39+
Console.WriteLine($"The object is some other type");
40+
break;
41+
}
42+
}
43+
#endregion
44+
45+
public static int CallTestType()
46+
{
47+
#region GenericTestTypeWithSwitch
48+
TestType(5);
49+
long longValue = 12;
50+
TestType(longValue);
51+
int? answer = 42;
52+
TestType(answer);
53+
double pi = 3.14;
54+
TestType(pi);
55+
string sum = "12";
56+
TestType(sum);
57+
answer = null;
58+
TestType(answer);
59+
string message = "This is a longer message";
60+
TestType(message);
61+
#endregion
62+
return 0;
63+
}
64+
}
65+
}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
3+
namespace ExploreCsharpSeven
4+
{
5+
6+
static class InReadonly
7+
{
8+
#region PointStructure
9+
public struct Point3D
10+
{
11+
private static Point3D origin = new Point3D(0, 0, 0);
12+
13+
public static Point3D Origin => origin;
14+
15+
public double X { get; }
16+
public double Y { get; }
17+
public double Z { get; }
18+
19+
private double? distance;
20+
21+
public Point3D(double x, double y, double z)
22+
{
23+
X = x;
24+
Y = y;
25+
Z = z;
26+
distance = null;
27+
}
28+
29+
public double ComputeDistance()
30+
{
31+
if (!distance.HasValue)
32+
distance = Math.Sqrt(X * X + Y * Y + Z * Z);
33+
return distance.Value;
34+
}
35+
36+
public static Point3D Translate(in Point3D source, double dX, double dY, double dZ) =>
37+
new Point3D(source.X + dX, source.Y + dY, source.Z + dZ);
38+
39+
public override string ToString()
40+
=> $"({X}, {Y}, {Z})";
41+
}
42+
#endregion
43+
44+
public static int ModifyTheOrigin()
45+
{
46+
#region UsePointstructure
47+
var start = Point3D.Origin;
48+
Console.WriteLine($"Start at the origin: {start}");
49+
50+
// Move the start:
51+
start = Point3D.Translate(in start, 5, 5, 5);
52+
Console.WriteLine($"Translate by (5,5,5): {start}");
53+
54+
Console.WriteLine($"Check the origin again: {Point3D.Origin}");
55+
#endregion
56+
return 0;
57+
}
58+
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace ExploreCsharpSeven
4+
{
5+
static class IsExpressions
6+
{
7+
public static int ExploreIsPattern()
8+
{
9+
#region IsTypePattern
10+
object count = 5;
11+
12+
if (count is int number)
13+
Console.WriteLine(number);
14+
else
15+
Console.WriteLine($"{count} is not an integer");
16+
#endregion
17+
return 0;
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ExploreCsharpSeven
6+
{
7+
class LocalFunctions
8+
{
9+
#region LocalFunctionFactorial
10+
public static int CalculateFactorial(int n)
11+
{
12+
return nthFactorial(n);
13+
14+
int nthFactorial(int number) => (number < 2) ?
15+
1 : number * nthFactorial(number - 1);
16+
}
17+
#endregion
18+
19+
public static int TestLocalFactorial()
20+
{
21+
#region LocalFunctionFactorialTest
22+
Console.WriteLine(CalculateFactorial(6));
23+
#endregion
24+
return 0;
25+
}
26+
27+
#region LocalFuntionIteratorMethod
28+
static IEnumerable<char> AlphabetSubset(char start, char end)
29+
{
30+
if (start < 'a' || start > 'z')
31+
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
32+
if (end < 'a' || end > 'z')
33+
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");
34+
35+
if (end <= start)
36+
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
37+
for (var c = start; c < end; c++)
38+
yield return c;
39+
}
40+
#endregion
41+
42+
#region LocalFunctionIteratorWithLocal
43+
static IEnumerable<char> AlphabetSubsetLocal(char start, char end)
44+
{
45+
if (start < 'a' || start > 'z')
46+
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
47+
if (end < 'a' || end > 'z')
48+
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");
49+
50+
if (end <= start)
51+
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
52+
53+
return alphabetSubsetImplementation();
54+
55+
IEnumerable<char> alphabetSubsetImplementation()
56+
{
57+
for (var c = start; c < end; c++)
58+
yield return c;
59+
}
60+
}
61+
#endregion
62+
63+
64+
65+
public static int TestSubset()
66+
{
67+
#region LocalFunctionsIteratorTest
68+
try
69+
{
70+
var resultSet1 = AlphabetSubset('d', 'r');
71+
var resultSet2 = AlphabetSubset('f', 'a');
72+
Console.WriteLine("iterators created");
73+
foreach (var thing1 in resultSet1)
74+
Console.Write($"{thing1}, ");
75+
Console.WriteLine();
76+
foreach (var thing2 in resultSet2)
77+
Console.Write($"{thing2}, ");
78+
Console.WriteLine();
79+
}
80+
catch (ArgumentException)
81+
{
82+
Console.WriteLine("Caught an argument exception");
83+
}
84+
return 1;
85+
#endregion
86+
}
87+
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace ExploreCsharpSeven
5+
{
6+
static class OutVariableDeclarations
7+
{
8+
public static int DeclareAtUse()
9+
{
10+
#region OutVariableDeclarations
11+
var input = "1234";
12+
if (int.TryParse(input, out int result))
13+
Console.WriteLine(result);
14+
else
15+
Console.WriteLine("Could not parse input");
16+
#endregion
17+
return 0;
18+
}
19+
20+
public static int ExploreScope()
21+
{
22+
#region OutVariableDeclarationScope
23+
var input = "1234";
24+
if (!int.TryParse(input, out int result))
25+
{
26+
Console.WriteLine("Could not parse input");
27+
return 1;
28+
}
29+
Console.WriteLine(result);
30+
#endregion
31+
return 0;
32+
}
33+
34+
public static int OutVarQuery()
35+
{
36+
#region DeclareOutQueryVariable
37+
string[] input = { "1", "2", "3", "4", "five", "6", "7" };
38+
var numbers = from s in input
39+
select (success: int.TryParse(s, out int result), result);
40+
foreach (var item in numbers)
41+
Console.WriteLine($"{(item.success ? "Success result is: " : "Failed to parse")}\t{(item.success ? item.result.ToString() : string.Empty)}");
42+
#endregion
43+
return 0;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)