Categories
Programming

C# 9 Covariant Return Types

Just a quick note on a neat feature in C# 9 that will allow sub-classes to return a covariant return type… what?! Okay, it allows you to return a more specific, or narrower type. For example, we used to have to return the same type… // parent class… public virtual Literature GetLiterature(…) { return new […]

Categories
Programming

C# 9 Non-Null Parameters

This neat C# 9 feature can be summed up in a tiny snippet of code. You know that code analysis warning that tells you that the argument passed might be null… wouldn’t it be nice if you could refuse to accept null? Well, you can. This is the before… public Book(string firstName, string lastName) And […]

Categories
Programming

C# 9 Record Types

We have taken a quick look at C# 9 Initializers and Immutability and C# 9 Non-Destructive Mutation. Let’s now look at the full transformation from an old class to a super character-light record type. Our original record type is very much just a class with two auto-properties. It looks different to a normal class because […]

Categories
Programming

C# 9 Non-Destructive Mutation

There are some phrases in programming that feel like they belong in a superhero movie. Non-destructive mutation is just such as phrase. It refers to a concept in functional programming that means when you want to change the state of an object, you create a copy with the change – rather than changing the original. […]

Categories
Programming

C# 9 Initializers and Immutability

There is currently a compromise in C# that means you can enable object initialization with getters and setters, or you can prevent external code mutating state by hiding the setter… but not both. C# 9 gives us both with the init keyword. Let’s look at before… public class Book { public string Author { get; […]

Categories
Programming

C# 9 Simplified Console Apps

At Microsoft Build 2020, an interesting new “see less boilerplate” feature from C# 9 was demonstrated. It removes all the code to create the class and static Main method, letting you just start typing your code. It makes your code way-less-nested (okay, two levels), but doesn’t miss any of your usual features. For example, args […]

Categories
Programming

Entity Type IdentityUserLogin Requires a Primary Key

You will usually come across this problem in .net Core Entity Framework when you first override the OnModelCreating method in your DbContext. The entity type ‘IdentityUserLogin‘ requires a primary key to be defined. If you intended to use a keyless entity type call ‘HasNoKey()’. If you check your DbContext class, you’ll see that when you […]

Categories
Programming

Check Certificate Expiry Date in .NET Core

I needed to check the certificate expiry date in an NUnit test. The below snippet can be used to check any certificate properties, using a custom certificate validation callback. All you need to do is read the properties you are interested in within the callback so you can check them afterwards. DateTime notAfter = DateTime.UtcNow; […]

Categories
Programming

Removing Special Characters and Diacritic Marks in C#

I did this trick in JavaScript to remove diacritic marks a while back and the need to perform a similar transformation in C# came up this week. The following method simplifies strings such as “façade” into simple string like “façade”. private static string Simplify(string input) { string normalizedString = input.Normalize(NormalizationForm.FormD); StringBuilder stringBuilder = new StringBuilder(); […]

Categories
Programming

Import IIS Log Files to SQL Server with Web Log Importer

In the past I have used Log Parser Studio to run SQL style queries against IIS logs, but it can take a fair bit of time to do this. Sometimes you just want to run a quick SQL query against the log data that IIS has collected. SQL Server is a super quick way of […]