c#-7.0

Deconstruction in foreach over Dictionary

江枫思渺然 提交于 2020-06-09 11:44:08
问题 Is it possible in C#7 to use deconstruction in a foreach-loop over a Dictionary? Something like this: var dic = new Dictionary<string, int>{ ["Bob"] = 32, ["Alice"] = 17 }; foreach (var (name, age) in dic) { Console.WriteLine($"{name} is {age} years old."); } It doesn't seem to work with Visual Studio 2017 RC4 and .NET Framework 4.6.2: error CS1061: 'KeyValuePair' does not contain a definition for 'Deconstruct' and no extension method 'Deconstruct' accepting a first argument of type

Why is a local function not always hidden in C#7?

 ̄綄美尐妖づ 提交于 2020-05-07 21:48:05
问题 What I am showing below, is rather a theoretical question. But I am interested in how the new C#7 compiler works and resolves local functions. In C#7 I can use local functions. For example (you can try these examples in LinqPad beta): Example 1: Nested Main() void Main() { void Main() { Console.WriteLine("Hello!"); } Main(); } DotNetFiddle for Example 1 Rather than calling Main() in a recursive way, the local function Main() is being called once, so the output of this is: Hello! The compiler

Why is a local function not always hidden in C#7?

假如想象 提交于 2020-05-07 21:48:04
问题 What I am showing below, is rather a theoretical question. But I am interested in how the new C#7 compiler works and resolves local functions. In C#7 I can use local functions. For example (you can try these examples in LinqPad beta): Example 1: Nested Main() void Main() { void Main() { Console.WriteLine("Hello!"); } Main(); } DotNetFiddle for Example 1 Rather than calling Main() in a recursive way, the local function Main() is being called once, so the output of this is: Hello! The compiler

Why is a local function not always hidden in C#7?

白昼怎懂夜的黑 提交于 2020-05-07 21:42:59
问题 What I am showing below, is rather a theoretical question. But I am interested in how the new C#7 compiler works and resolves local functions. In C#7 I can use local functions. For example (you can try these examples in LinqPad beta): Example 1: Nested Main() void Main() { void Main() { Console.WriteLine("Hello!"); } Main(); } DotNetFiddle for Example 1 Rather than calling Main() in a recursive way, the local function Main() is being called once, so the output of this is: Hello! The compiler

Why is a local function not always hidden in C#7?

て烟熏妆下的殇ゞ 提交于 2020-05-07 21:42:30
问题 What I am showing below, is rather a theoretical question. But I am interested in how the new C#7 compiler works and resolves local functions. In C#7 I can use local functions. For example (you can try these examples in LinqPad beta): Example 1: Nested Main() void Main() { void Main() { Console.WriteLine("Hello!"); } Main(); } DotNetFiddle for Example 1 Rather than calling Main() in a recursive way, the local function Main() is being called once, so the output of this is: Hello! The compiler

Using named tuples in select statements

旧街凉风 提交于 2020-04-10 07:13:05
问题 Is there a nicer way to select a named tuple in C# 7 using a var target variable? I must be doing something wrong in example 1, or misunderstanding something completely. I seem to have to explicitly set the target type in order to do this. //1. Fails to compile with "incorrect number of type parameters" issue. var tuples = source.Select<(int A, int B)>(x => (x.A, x.B)); //2. Compiles IEnumerable<(int A, int B)> tuples = toCheck.Select(x => (x.A, x.B)); //3. Compiles var tuples = new HashSet<

Using named tuples in select statements

五迷三道 提交于 2020-04-10 07:09:07
问题 Is there a nicer way to select a named tuple in C# 7 using a var target variable? I must be doing something wrong in example 1, or misunderstanding something completely. I seem to have to explicitly set the target type in order to do this. //1. Fails to compile with "incorrect number of type parameters" issue. var tuples = source.Select<(int A, int B)>(x => (x.A, x.B)); //2. Compiles IEnumerable<(int A, int B)> tuples = toCheck.Select(x => (x.A, x.B)); //3. Compiles var tuples = new HashSet<

Switching on type with a generic return type

旧巷老猫 提交于 2020-02-14 17:25:47
问题 I'm working on making EF easier to unit test by writing some helpers that will make properties for me. I have a couple of backing fields private Mock<DbSet<Workflow>> mockedWorkFlows; private Mock<DbSet<WorkflowError>> mockedWorkFlowErrors; And I want a generic function to be able to return me the correct backing field with the following function public Mock<DbSet<T>> Mocked<T>(T t) where T : class { if ( (object)t is Workflow) { return mockedWorkFlows; //cannot Workflow to T } } There are

Why Local Functions generate IL different from Anonymous Methods and Lambda Expressions?

╄→尐↘猪︶ㄣ 提交于 2020-01-22 17:36:06
问题 Why the C# 7 Compiler turns Local Functions into methods within the same class where their parent function is. While for Anonymous Methods (and Lambda Expressions) the compiler generates a nested class for each parent function, that will contain all of its Anonymous Methods as instance methods ? For example, C# code (Anonymous Method) : internal class AnonymousMethod_Example { public void MyFunc(string[] args) { var x = 5; Action act = delegate () { Console.WriteLine(x); }; act(); } } Will