This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var glory = new NewAndGlory(); | |
string s1 = glory.GetANullString(); | |
string s2 = glory.GetAString(); | |
string s3 = glory.PassAString(null); // having a NullReferenceException here! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static string GetIsbn1(Book book) | |
{ | |
string? isbn = book.Isbn; | |
if (isbn == null) | |
{ | |
return string.Empty; | |
} | |
return isbn; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public string GetIsbn2(Book book) | |
=> book.Isbn ?? string.Empty; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeClass : ILegacyInterface | |
{ | |
public string? Foo() => null; | |
} | |
class AnotherClass : ILegacyInterface | |
{ | |
public string Foo() => "a string"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Legacy | |
{ | |
public string GetANullString() => null; | |
public string PassAString(string s) | |
{ | |
if (s == null) throw new ArgumentNullException(nameof(s)); | |
return s.ToUpper(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "1.0", | |
"defaultProvider": "cdnjs", | |
"libraries": [ | |
{ | |
"library": "jquery@3.3.1", | |
"destination": "wwwroot/lib/jQuery", | |
"files": ["jquery.js", "jquery.min.js", "jquery.min.map"] | |
} | |
] |