Skip to content

Commit 9cdb210

Browse files
committed
c# 7 Initial Commit
0 parents  commit 9cdb210

21 files changed

+672
-0
lines changed

.gitignore

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
## Ignore Visual Studio temporary files, build results, and
2+
## files generated by popular Visual Studio add-ons.
3+
4+
# User-specific files
5+
*.suo
6+
*.user
7+
*.sln.docstates
8+
9+
# Build results
10+
[Dd]ebug/
11+
[Dd]ebugPublic/
12+
[Rr]elease/
13+
x64/
14+
build/
15+
bld/
16+
[Bb]in/
17+
[Oo]bj/
18+
19+
# MSTest test Results
20+
[Tt]est[Rr]esult*/
21+
[Bb]uild[Ll]og.*
22+
23+
#ignore thumbnails created by windows
24+
Thumbs.db
25+
#Ignore files build by Visual Studio
26+
*.obj
27+
*.exe
28+
*.pdb
29+
*.user
30+
*.aps
31+
*.pch
32+
*.vspscc
33+
*_i.c
34+
*_p.c
35+
*.ncb
36+
*.suo
37+
*.tlb
38+
*.tlh
39+
*.bak
40+
41+
*.cache
42+
43+
*.ilk
44+
45+
*.log
46+
47+
*.orig
48+
49+
# Windows image file caches
50+
Thumbs.db
51+
ehthumbs.db
52+
53+
# Visual Studio profiler
54+
*.psess
55+
*.vsp
56+
*.vspx
57+
58+
# ReSharper is a .NET coding add-in
59+
_ReSharper*/
60+
*.[Rr]e[Ss]harper
61+
*.DotSettings.user
62+
[Tt]est[Rr]esult*
63+
64+
# NuGet Packages Directory
65+
packages/
66+
## TODO: If the tool you use requires repositories.config uncomment the next line
67+
#!packages/repositories.config
68+
69+
# Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
70+
# This line needs to be after the ignore of the build folder (and the packages folder if the line above has been uncommented)
71+
!packages/build/
72+
73+
# Windows Store app package directory
74+
AppPackages/
75+
76+
77+
78+
# Microsoft Fakes
79+
FakesAssemblies/
80+
81+
# Backup & report files from converting an old project file to a newer
82+
# Visual Studio version. Backup files are not needed, because we have git ;-)
83+
_UpgradeReport_Files/
84+
Backup*/
85+
UpgradeLog*.XML
86+
UpgradeLog*.htm
87+
/.gitconfig

CSharp7Console.sln

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26206.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharp7Console", "CSharp7Console\CSharp7Console.csproj", "{FC109014-DEDF-4C6B-886A-4651D6EF3539}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{557D7F3D-0561-492A-B5BC-C4622144F450}"
9+
ProjectSection(SolutionItems) = preProject
10+
readme.md = readme.md
11+
EndProjectSection
12+
EndProject
13+
Global
14+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
15+
Debug|Any CPU = Debug|Any CPU
16+
Release|Any CPU = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{FC109014-DEDF-4C6B-886A-4651D6EF3539}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{FC109014-DEDF-4C6B-886A-4651D6EF3539}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{FC109014-DEDF-4C6B-886A-4651D6EF3539}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{FC109014-DEDF-4C6B-886A-4651D6EF3539}.Release|Any CPU.Build.0 = Release|Any CPU
23+
EndGlobalSection
24+
GlobalSection(SolutionProperties) = preSolution
25+
HideSolutionNode = FALSE
26+
EndGlobalSection
27+
EndGlobal

CSharp7Console/01Tuples.cs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
3+
namespace CSharp7Console
4+
{
5+
public class Tuples
6+
{
7+
8+
//Currently tuples are ref types, and immutable, meaning each time you change a value a new object is created
9+
//this makes them very inefficient, but thread safe.
10+
//New Tuples are value types, making them still thread safe, but now more efficient.
11+
12+
private static Tuple<int, string> GetEmployeeByIdExisting(int employeeId)
13+
{
14+
var empAge = 32;
15+
var dept = "HR";
16+
return new Tuple<int, string>(empAge, dept);
17+
}
18+
19+
//Shortened syntax for the tuple
20+
private static (int, string) GetEmployeeByIdShortenedSyntax(int employeeId)
21+
{
22+
var empAge = 32;
23+
var dept = "HR";
24+
return (empAge, dept);
25+
}
26+
27+
//Shortened named fields in the tuple
28+
private static (int age, string department) GetEmployeeByIdNamedFields(int employeeId)
29+
{
30+
var empAge = 32;
31+
var dept = "HR";
32+
return (empAge, dept);
33+
}
34+
35+
public static void TupleExample()
36+
{
37+
var employeeTuple = GetEmployeeByIdExisting(1);
38+
var age = employeeTuple.Item1;
39+
var dept = employeeTuple.Item2;
40+
41+
//newer approach with shortened syntax..
42+
var employeeShortSyntaxTuple = GetEmployeeByIdShortenedSyntax(1);
43+
age = employeeShortSyntaxTuple.Item1;
44+
dept = employeeShortSyntaxTuple.Item2;
45+
46+
//newer approach with named fields..
47+
var employee = GetEmployeeByIdNamedFields(1);
48+
age = employee.age;
49+
dept = employee.department; //bug reverts to items2
50+
dept = employee.Item2; //intelli sense is screwed up..
51+
52+
Console.WriteLine($"{age} {dept}");
53+
}
54+
55+
}
56+
}

CSharp7Console/02Deconstruction.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
namespace CSharp7Console
4+
{
5+
public class Deconstruction
6+
{
7+
//Shortened named fields in the tuple
8+
private static (int age, string department) GetEmployeeByIdNamedFields(int employeeId)
9+
{
10+
var empAge = 32;
11+
var dept = "HR";
12+
return (empAge, dept);
13+
}
14+
15+
public static void DeconstructionExample()
16+
{
17+
//deconstruct immediately and remove the need to hold onto the tuple.
18+
(int age, string dept) = GetEmployeeByIdNamedFields(1);
19+
Console.WriteLine($"{age} {dept}");
20+
21+
//or with var
22+
(var employeeAge, var employeeDept) = GetEmployeeByIdNamedFields(1);
23+
Console.WriteLine($"{employeeAge} {employeeDept}");
24+
25+
26+
//Even lazier var...
27+
var (employeeAge2, employeeDept2) = GetEmployeeByIdNamedFields(1);
28+
Console.WriteLine($"{employeeAge2} {employeeDept2}");
29+
}
30+
31+
}
32+
}

CSharp7Console/03LocalFunctions.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace CSharp7Console
4+
{
5+
public class LocalFunctions
6+
{
7+
/// <summary>
8+
/// Embed a function within a function, this gives the advantage that no other method can call the embedded function.
9+
/// You can't test the embedded
10+
/// </summary>
11+
/// <returns></returns>
12+
private static int GetSalary()
13+
{
14+
var salary = GetBonus() * 2;
15+
16+
return salary;
17+
18+
int GetBonus()
19+
{
20+
return 10000;
21+
}
22+
}
23+
24+
public static void LocalFunctionExample()
25+
{
26+
Console.WriteLine("Salary is: " + GetSalary());
27+
}
28+
}
29+
}

CSharp7Console/04OutParams.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
3+
namespace CSharp7Console
4+
{
5+
public class OutParams
6+
{
7+
public static void GetDetails(out int userId, out string username)
8+
{
9+
userId = 12;
10+
username = "Test User";
11+
}
12+
13+
private static void OutParamsExample()
14+
{
15+
//As it currently stands
16+
int userId;
17+
string userName;
18+
GetDetails(out userId, out userName);
19+
20+
//new method
21+
GetDetails(out int userIdNew, out string userNameNew);
22+
Console.WriteLine($"{userIdNew} {userNameNew}");
23+
24+
//Use var instead of actual type..
25+
GetDetails(out var userIdVar, out var userNameVar);
26+
27+
//Talk of allowing *, but not implemented.
28+
//GetDetails(out var userIdVar, *);
29+
30+
}
31+
32+
}
33+
}

CSharp7Console/05PatternMatching.cs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+

2+
using System;
3+
4+
namespace CSharp7Console
5+
{
6+
public class PatternMatching
7+
{
8+
private enum ShapeType
9+
{
10+
Circle,
11+
Rectange
12+
}
13+
14+
private class Shape { }
15+
private class Circle : Shape { public int Radius => 100; }
16+
private class Rectangle : Shape
17+
{
18+
public int Length => 100;
19+
public int Height => 120;
20+
}
21+
22+
private static Shape GetShapeFactory(ShapeType style)
23+
{
24+
switch (style)
25+
{
26+
case ShapeType.Circle:
27+
return new Circle();
28+
case ShapeType.Rectange:
29+
return new Rectangle();
30+
default:
31+
return null;
32+
}
33+
}
34+
35+
public static void SwitchPatternMatchingExample()
36+
{
37+
var shape = GetShapeFactory(ShapeType.Rectange);
38+
39+
switch (shape)
40+
{
41+
case Circle c:
42+
Console.WriteLine($"circle with radius {c.Radius}");
43+
break;
44+
case Rectangle s when (s.Length == s.Height):
45+
Console.WriteLine($"{s.Length} x {s.Height} square");
46+
break;
47+
case Rectangle r:
48+
Console.WriteLine($"{r.Length} x {r.Height} rectangle");
49+
break;
50+
default:
51+
Console.WriteLine("<unknown shape>");
52+
break;
53+
case null:
54+
throw new ArgumentNullException(nameof(shape));
55+
}
56+
}
57+
58+
private static object GetMultipler()
59+
{
60+
return 100;
61+
}
62+
63+
public static void PatternMatchingExample()
64+
{
65+
var o = GetMultipler();
66+
67+
if (o is null) return;
68+
69+
if (o is int i || o is string s && int.TryParse(s, out i))
70+
{
71+
//Why is s not valid??
72+
//Console.WriteLine($"Multipler is a string with the value {s}");
73+
74+
Console.WriteLine($"Multipler is set {i} {new string('*', i)}");
75+
}
76+
77+
//Illegal as outside the scope of the braces.
78+
//Console.WriteLine($"Multipler is set {i}");
79+
}
80+
}
81+
}

CSharp7Console/06NullRefTest.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace CSharp7Console
5+
{
6+
public class NullRefTest
7+
{
8+
public static void NullRefTestExample()
9+
{
10+
StringBuilder stringBuilder = new StringBuilder();
11+
stringBuilder = null;
12+
13+
Console.WriteLine(stringBuilder.Capacity); // the ide will now say that stringbuilder is null, no need to guess anymore..
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace CSharp7Console
4+
{
5+
public class BinaryLiteralsAndSeparaters
6+
{
7+
public static void BinaryLiteralsExample()
8+
{
9+
var binary = new[] {0b1, 0b10, 0b11, 0b100, 0b101, 0b110, 0b111, 0x8, 0xF, 32};
10+
11+
foreach (var binValue in binary)
12+
{
13+
Console.WriteLine(binValue);
14+
}
15+
16+
17+
#region//_ separator
18+
var separaters = new int[] { 1_01, 1_02, 1_03, 1_000_000, 1_000_000_000 };
19+
foreach (var value in separaters)
20+
{
21+
Console.WriteLine(value);
22+
}
23+
#endregion
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)