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 / 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;
@christiannagel
christiannagel / deconstructors.cs
Created May 5, 2018 21:52
deconstruction of a tuple and a Person class
(int n, string s) = (42, "magic");
var p1 = new Person("Tom", "Turbo");
(string firstName, string lastName) = p1;
@christiannagel
christiannagel / PatternMatching.cs
Created May 5, 2018 21:53
Pattern matching with C# 7
public void PatternMatchingWithIsOperator(object o)
{
if (o is 42)
{
}
if (o is Person p)
{
}
if (o is var v1)
{
@christiannagel
christiannagel / ThrowExpressions.cs
Created May 5, 2018 21:55
throw expressions with C# 7
// C# 6
private readonly IBooksService _booksService;
public BookController(BooksService booksService)
{
if (booksService == null)
{
throw new ArgumentNullException(nameof(b));
}
_booksService = booksService;
@christiannagel
christiannagel / AsyncMain.cs
Created May 5, 2018 21:57
async main with C# 7.1
// C# 7.0
static void Main()
{
SomeMethodAsync().Wait();
}
// C# 7.1
async static Task Main()
{
await SomeMethodAsync();
@christiannagel
christiannagel / ReferenceSemantics.cs
Created May 5, 2018 22:00
ref returns, ref locals, ref readonly, and ref struct with C# 7
// ref returns and ref locals with C# 7.0
int[] _numbers = { 3, 7, 11, 15, 21 };
public ref int GetNumber(int index)
{
return ref _numbers[index];
}
// ref readonly with C# 7.2
int[] _numbers = { 3, 7, 11, 15, 21 };
public ref readonly int GetNumber(int index)
@christiannagel
christiannagel / Privacy.cshtml
Created May 17, 2018 12:16
Template generated privacy policy
@{
ViewData["Title"] = "Privacy Policy";
}
<h2>@ViewData["Title"]</h2>
<p>Use this page to detail your site's privacy policy.</p>
@christiannagel
christiannagel / Startup.cs
Created May 24, 2018 09:20
Configuration of Services, storing users in the database - ASP.NET Core 2.1
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>