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 / Shape.cs
Created June 19, 2018 11:27
Shape class used by pattern matching samples
public abstract class Shape
{
public (int x, int y) Position { get; }
public (int height, int width) Size { get; }
public Shape((int x, int y) position, (int height, int width) size)
=> (Position, Size) = (position, size);
public void Deconstruct(out (int x, int y) position, out (int x, int y) size)
=> (position, size) = (Position, Size);
@christiannagel
christiannagel / Program.cs
Created June 19, 2018 11:23
C# 7 pattern matching with the switch statement and type patterns with a filter
static string M1(Shape shape)
{
switch (shape)
{
case Shape s when s.Size.height > 100:
return $"large shape with size {s.Size} at position {s.Position}";
case Ellipse e:
return $"Ellipse with size {e.Size} at position {e.Position}";
case Rectangle r:
return $"Rectangle with size {r.Size} at position {r.Position}";
@christiannagel
christiannagel / Program.cs
Created June 15, 2018 06:06
Using a C# 8 library from a C# 7 application
var glory = new NewAndGlory();
string s1 = glory.GetANullString();
string s2 = glory.GetAString();
string s3 = glory.PassAString(null); // having a NullReferenceException here!
@christiannagel
christiannagel / Program.cs
Last active June 15, 2018 05:53
Assigning nullable to non-nullable with C# 8
static string GetIsbn1(Book book)
{
string? isbn = book.Isbn;
if (isbn == null)
{
return string.Empty;
}
return isbn;
}
@christiannagel
christiannagel / Program.cs
Created June 15, 2018 05:51
Assigning nullable to non-nullable with C# 8
public string GetIsbn2(Book book)
=> book.Isbn ?? string.Empty;
@christiannagel
christiannagel / Program.cs
Created June 14, 2018 15:12
Different implementations of C# 7 defined interfaces regarding to nullability
class SomeClass : ILegacyInterface
{
public string? Foo() => null;
}
class AnotherClass : ILegacyInterface
{
public string Foo() => "a string";
}
@christiannagel
christiannagel / Program.cs
Created June 14, 2018 06:54
Using old and new libraries from C# 8 applications
var newglory = new NewAndGlory();
string? s1 = newglory.GetANullString();
string s2 = newglory.GetAString();
// string s3 = newglory.PassAString(null); // error: cannot convert null literal to non-nullable reference or unconstrained type parameter
var old = new Legacy();
string s4 = old.GetANullString(); // no error, s1 is null!
string s5 = old.PassAString(null); // no error
@christiannagel
christiannagel / Book.cs
Last active June 14, 2018 06:43
C# 8 Book Class
class Book
{
public string Title { get; }
public string Publisher { get; }
public string? Isbn { get; }
public Book(string title, string publisher, string? isbn)
=> (Title, Publisher, Isbn) = (title, publisher, isbn);
public Book(string title, string publisher)
: this(title, publisher, null) { }
@christiannagel
christiannagel / Legacy.cs
Created June 14, 2018 06:24
C# 7 version with nullability
public class Legacy
{
public string GetANullString() => null;
public string PassAString(string s)
{
if (s == null) throw new ArgumentNullException(nameof(s));
return s.ToUpper();
}
}
@christiannagel
christiannagel / libman.json
Created June 13, 2018 06:47
libman.json referencing jquery
{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": [
{
"library": "jquery@3.3.1",
"destination": "wwwroot/lib/jQuery",
"files": ["jquery.js", "jquery.min.js", "jquery.min.map"]
}
]