Skip to content

Instantly share code, notes, and snippets.

Avatar

Christian Nagel christiannagel

View GitHub Profile
@christiannagel
christiannagel / Program.cs
Last active Jun 15, 2018
Assigning nullable to non-nullable with C# 8
View Program.cs
static string GetIsbn1(Book book)
{
string? isbn = book.Isbn;
if (isbn == null)
{
return string.Empty;
}
return isbn;
}
@christiannagel
christiannagel / Program.cs
Created Jun 14, 2018
Different implementations of C# 7 defined interfaces regarding to nullability
View Program.cs
class SomeClass : ILegacyInterface
{
public string? Foo() => null;
}
class AnotherClass : ILegacyInterface
{
public string Foo() => "a string";
}
@christiannagel
christiannagel / Program.cs
Created Jun 14, 2018
Using old and new libraries from C# 8 applications
View Program.cs
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 Jun 14, 2018
C# 8 Book Class
View Book.cs
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 Jun 14, 2018
C# 7 version with nullability
View Legacy.cs
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 Jun 13, 2018
libman.json referencing jquery
View libman.json
{
"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 Jun 13, 2018
Created libman.json file by using "Manage Client-Side Libraries..."
View libman.json
{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": []
}
@christiannagel
christiannagel / NewAndGlory.cs
Created Jun 10, 2018
Passing and returning non-nullable strings with C# 8
View NewAndGlory.cs
public class NewAndGlory
{
public string? GetANullString() => null;
public string GetAString() => "a string";
public string PassAString(string s) => s.ToUpper();
}
@christiannagel
christiannagel / Nullability.cs
Created Jun 10, 2018
Nullability with C# 8
View Nullability.cs
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 Jun 8, 2018
Add a HttpClientHandler to a HTTP Client Factory configuration
View UseClientHandlerSample.cs
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 =>
{
You can’t perform that action at this time.