Skip to content

Instantly share code, notes, and snippets.

View christiannagel's full-sized avatar
💭
working on the new book Pragmatic Microservices with C# and Azure

Christian Nagel christiannagel

💭
working on the new book Pragmatic Microservices with C# and Azure
View GitHub Profile
@christiannagel
christiannagel / binaryliterals.cs
Created May 5, 2018 21:12
binary literals with C# 7
uint binary1 = 0b1111_0000_1010_0101_1111_0000_1010_0101;
@christiannagel
christiannagel / expressionbodiedmembers.cs
Created May 5, 2018 21:14
Expression-bodied members with C# 7
// C# 6
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { Set(ref _firstName, value); }
}
// C# 7
private string _firstName;
@christiannagel
christiannagel / outvar.cs
Created May 5, 2018 21:17
out var with C# 7
// C# 6
string n = "42";
int result;
if (string.TryParse(n, out result)
{
Console.WriteLine($"Converting to a number was successful: {result}");
}
// C# 7
string n = "42";
@christiannagel
christiannagel / NonTrailingNamedArguments.cs
Created May 5, 2018 21:31
Non-trailing named arguments with C# 7.2
// C# 7.0
if (Enum.TryParse(weekdayRecommendation.Entity, ignoreCase: true,
result: out DayOfWeek weekday))
{
reservation.Weekday = weekday;
}
// C# 7.2
if (Enum.TryParse(weekdayRecommendation.Entity, ignoreCase: true,
out DayOfWeek weekday))
@christiannagel
christiannagel / ReadonlyStruct.cs
Last active May 5, 2018 21:32
readonly struct with C# 7.2
public readonly struct Dimensions
{
public double Length { get; }
public double Width { get; }
public Dimensions(double length, double width)
{
Length = length;
Width = width;
}
@christiannagel
christiannagel / InModifier.cs
Created May 5, 2018 21:36
in modifier with C# 7.2
static void CantChange(in AStruct s)
{
// s can’t change
}
@christiannagel
christiannagel / defaultliteral.cs
Last active May 5, 2018 21:40
default literal with C# 7.1
// C# 7.0
int x = default(int);
ImmutableArray<int> arr = default(ImmutableArray<int>);
// C# 7.1
int x = default;
ImmutableArray<int> arr = default;
@christiannagel
christiannagel / LocalFunctions.cs
Created May 5, 2018 21:43
Local functions with C# 7
// C# 6
public void SomeFunStuff()
{
Func<int, int, int> add = (x, y) => x + y;
int result = add(38, 4);
Console.WriteLine(result);
}
// C# 7
@christiannagel
christiannagel / Tuples.cs
Created May 5, 2018 21:47
Tuples with C# 7
// C# 6
var t1 = Tuple.Create(42, "astring");
int i1 = t1.Item1;
string s1 = t1.Item2;
// C# 7
var t1 = (n: 42, s: "magic");
int i1 = t1.n;
string s1 = t1.s;
// C# 7.0
var t1 = (FirstName: racer.FirstName, Wins: racer.Wins);
int wins = t1.Wins;
// C# 7.1
var t1 = (racer.FirstName, racer.Wins);
int wins = t1.Wins;