Enabling the latest version of C#

June 21, 2018 c#, msbuild

As of this writing when creating a new console project via dotnet new console the version of C# used in the project is version 7.0. This means you're missing out cool features like Default Literal Expressions. The language version can be changed via Visual Studio though I prefer to enable it via MSBuild. This can be done in the csproj file or globally via a Build.Directory.props file. Changing the C# language version is done via a property known as LangVersion.


This property can set to any of the values listed here. As of this writing VS Code doesn't seem to like it when you use 7.3 instead of latest. VS Code will show errors in your code although the code will compile without any problems.

A neat way to curry in C#

May 25, 2018 c#, fp

I was reading an older blogpost from Mike Hadlow about Partial Application in C# in which he discusses how Partial Application can be implemented in C# via Currying. Although I appreciate his example of implementing currying via extsion methods, the syntax is hideous. There is a suggestion in the comments though that I found to be a much better solution.

// Define a local function Add. int Add(int a, int b) => a + b; // Here we do the currying. Func<int, int> add3 = (b) => Add(3, b); // This will print 5. Console.WriteLine(add3(2)); // Curry one more time so that we have // a function that simply produces 5. Func<int> five = () => add3(2); // This will also print 5. Console.WriteLine(five());

Lambda Collection Wrappers

April 26, 2018 c#, lambda, fp

I've been reading a lot as of late about functional programming and I try to incorperate as much as possible into my everyday programming. One trick that I've definitely started using is wrapping collections in lambda functions.

Read More

string.Join Extension Method

April 19, 2018 c#, linq

Normally if want to do a string.Join on the result of a Linq query you end up with somthing looking like this:

Console.WriteLine(string.Join(", ", numbers.Where(x => x % 2 == 0)));

The call to string.Join has to come first and then the Linq query. I've always felt this breaks the flow of the code and would be easier to read if the string.Join was at the end of the Linq query:

Console.WriteLine(numbers.Where(x => x % 2 == 0).JoinString(", "));

This can be implemented with the following extension methods:

public static class JoinStringExtensions { public static string JoinString<T>(this IEnumerable<T> source, string seperator) => string.Join(seperator, source.Select(x => x.ToString())); public static string JoinString(this IEnumerable<string> source, string seperator) => string.Join(seperator, source); }

The specialization for IEnumerable<string> is just mirroring the implementation from string.Join.

Run a program directly after building in VS Code

April 11, 2018 .net, .netcore, msbuild, vscode

Whenever I have a program that is just a generator of some kind I like to have that program execute directly after having built the program successfully. I'm going to show how to run the program via the dotnet cli but this trick can easily be applied to regular .NET programs or any program which is built using MSBuild.

Read More