Skip to content

Instantly share code, notes, and snippets.

View christiannagel's full-sized avatar

Christian Nagel christiannagel

View GitHub Profile
@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 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"]
}
]
@christiannagel
christiannagel / libman.json
Created June 13, 2018 06:41
Created libman.json file by using "Manage Client-Side Libraries..."
{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": []
}
@christiannagel
christiannagel / NewAndGlory.cs
Created June 10, 2018 09:16
Passing and returning non-nullable strings with C# 8
public class NewAndGlory
{
public string? GetANullString() => null;
public string GetAString() => "a string";
public string PassAString(string s) => s.ToUpper();
}
@christiannagel
christiannagel / Nullability.cs
Created June 10, 2018 08:53
Nullability with C# 8
int i1 = 4; // null is not allowed
int? i2 = null; // null is allowed
string s1 = "a string"; // null is not allowed
string? s2 = null; // a nullable string
@christiannagel
christiannagel / UseClientHandlerSample.cs
Created June 8, 2018 08:02
Add a HttpClientHandler to a HTTP Client Factory configuration
private ServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services.AddLogging(builder =>
{
builder.AddFilter((category, level) => true);
builder.AddConsole(options => options.IncludeScopes = true);
});
services.AddHttpClient("cni", client =>
{