Deconstruction in foreach over Dictionary

If you don’t like having to write the Deconstruct method, especially if you only need it in one place, here’s how to do it as a one-liner with LINQ: Using your original dictionary: var dic = new Dictionary<string, int>{ [“Bob”] = 32, [“Alice”] = 17 }; You can do it like this: foreach (var (name, … Read more

How to return multiple values in C# 7? [closed]

What do you mean by natively? C# 7 has a new feature that lets you return more than one value from a method thanks to tuple types and tuple literals. Take the following function for instance: (string, string, string) MyCoolFunction() // tuple return type { //… return (firstValue, secondValue, thirdValue); } Which can be used … 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

C# 7 tuples and lambdas

As you have observed, for: var list = new List<(int,int)>(); One would at least expect to be able to do the following: list.Select((x,y) => x*2 + y/2); But the C# 7 compiler doesn’t (yet) support this. It is also reasonable to desire sugar that would allow the following: void Foo(int x, int y) => … … 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

TryParse with out var param

A new feature in C# 6.0 allows to declare variable inside TryParse method. Declaration expressions was cut from C# 6.0 and wasn’t shipped in the final release. You currently can’t do that. There is a proposal for it on GitHub for C# 7 (also see this for future reference). Update (07/03/2017) With the official release … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)