C# 7 Expression Bodied Constructors

A way to do this is to use a tuple and a deconstruction to allow multiple assignments in one expression: public class Person { public string Name { get; } public int Age { get; } public Person(string name, int age) => (Name, Age) = (name, age); } As of C# 7.1 (introduced with Visual … Read more

Does C# 7 have array/enumerable destructuring?

It turns out not only tuples can be deconstructed but any type which has Deconstruct static (or extension) method with matching signature. Doing deconstruction correctly for IEnumerable is not trivial (see library suggested by David Arno in this answer), so let’s see how it works with simple IList instead (implementation is irrelevant, this one is … Read more

In C#7, how can I “roll my own” Task-like type to use with async?

I couldn’t find any good tutorial yet. But you can look at the compiler unittests which create such task-like types (look for “[AsyncMethodBuilder”). The starting point is to create a type and mark it as task-like with an attribute like [AsyncMethodBuilder(typeof(MyTaskBuilder))]. Then you need to define your own MyTaskBuilder type. It must implement a certain … Read more

In C# 7 is it possible to deconstruct tuples as method arguments

You can shorten it to: void test( Action<ValueTuple<string, int>> fn) { fn((“hello”, 10)); } test(((string s, int i) t) => { Console.WriteLine(t.s); Console.WriteLine(t.i); }); Hopefully, one day we might be able to splat the parameters from a tuple to the method invocation: void test(Action<ValueTuple<string, int>> fn) { fn(@(“hello”, 10)); // <– made up syntax } … Read more

Odd return syntax statement

This is C# 7.0 which supports local functions…. public static IEnumerable<TSource> DistinctBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); // This is basically executing _LocalFunction() return _LocalFunction(); // This is a new inline method, // return within this … Read more

Could not load file or assembly ‘System.ValueTuple’

ok this feels completely wrong but I cut <dependentAssembly> <assemblyIdentity name=”System.ValueTuple” publicKeyToken=”cc7b13ffcd2ddd51″ culture=”neutral” /> <bindingRedirect oldVersion=”0.0.0.0-4.0.3.0″ newVersion=”4.0.3.0″ /> </dependentAssembly> This out of my web.config for the main application. I was really just seeing what happened to see if there was an underlying dependency or something, not expecting it to run. It just carried on working, … Read more

Does C# 7.0 work for .NET 4.5?

Let’s go through the features new in C# 7.0: Tuples: The System.ValueTuple package has a version for the portable-net40+sl4+win8+wp8 profile. That means it is usable on .Net 4.0. (Not sure why dependencies list only .Net 4.5.) If you wanted to use tuples on even lower versions of .Net, it should still work, as long as … Read more