c#-7.0

C# deconstruction and overloads

喜夏-厌秋 提交于 2019-11-29 13:59:41
While investigating the new features in C# 7.x, I created the following class: using System; namespace ValueTuples { public class Person { public string Name { get; } public DateTime BirthDate { get; } public Person(string name, DateTime birthDate) { Name = name; BirthDate = birthDate; } public void Deconstruct(out string name, out int year, out int month, out int day) { name = Name; year = BirthDate.Year; month = BirthDate.Month; day = BirthDate.Day; } public void Deconstruct(out string name, out int year, out int month, out (int DayNumber, DayOfWeek DayOfWeek) day) { name = Name; year =

How do I get the new async semantics working in VS2017 RC?

守給你的承諾、 提交于 2019-11-29 13:42:25
问题 Quoting from Visual Studio 2017 RC Release Notes Language Extensions and Analyzers This release includes some proposed new language extensions that we are working on for the next versions of C# and Visual Basic. These new language features are enabled by default and include: For C#: Task-like return types for async methods: This introduces the ability to return any task-like type from an async method. Previously these return types were constrained to Task<T> and Task . It says it's enabled by

C# 7 Compiler Error - Pattern Matching

徘徊边缘 提交于 2019-11-29 10:55:11
问题 For some reason, M1() causes a compiler error, while M2() , which does the same thing, causes no error. Any idea why? Using false == should be the same as using the not operator, ! . Use of unassigned local variable 'i' class Program { static void Main(string[] args) { int x = 8; M1(x); M2(x); } // Main() public static void M1(Object obj) { if (false == (obj is int i)) // Causes ERROR on WriteLine return; System.Console.WriteLine(i); // Use of unassigned local variable 'i' } public static

Does C# 7 have array/enumerable destructuring?

不羁岁月 提交于 2019-11-29 10:36:08
问题 In Javascript ES6, you are able to destructure arrays like this: const [a,b,...rest] = someArray; where a is the first element in the array, b is the second, and rest is an array with the remaining elements. I know in C#7 that you can destructure tuples during assignment, but could not find anything related to destructuring arrays/enumerables like this: var (a,b) = someTuple; I have an IEnumerable where I need the first and second elements as variables, and I need the rest of the elements as

I can't get parameter names from valuetuple via reflection in c# 7.0

时光总嘲笑我的痴心妄想 提交于 2019-11-29 10:11:55
I want to Map a ValueTuple to a class using reflection. Documentation says that there is a Attribute attached to ValueTuple with parameters names (others than Item1, Item2, etc...) but I can't see any Attribute. Disassembly shows nothing. What's happens? Example: public static T ToStruct<T, T1,T2>(this ValueTuple<T1,T2> tuple) where T : struct Via reflection can't get Item1, Item2 names to match with T fields via reflection. You should have the TupleElementNames attribute on the method created by the compiler. See this code : public class C { public (int a, int b) M() { return (1, 2); } }

Non-shortcircuting boolean operators and C# 7 Pattern matching

柔情痞子 提交于 2019-11-29 06:35:52
Im currently writing an C# application, targeting .NET 4.7 (C# 7). I am confused after I tried using the new way of declaring a variable utilizing the "is" keyword: if (variable is MyClass classInstance) This way it works, but when doing: if (true & variable is MyClass classInstance) { var a = classInstance; } Visual Studio (I'm using 2017) shows me the the Error Use of unassigned local variable 'classInstance' . Using the short-circuting version of & ( && ) it works fine. Am I missing something about the & operator? (I know using the shortcircuting versions are much more commonly used, but at

How to build .csproj with C# 7 code from command line (msbuild)

半腔热情 提交于 2019-11-29 06:24:12
问题 I use some C# 7 features in my project: static void Main(string[] args) { } public byte ContainerVersion { get => 1; private set => throw new NotImplementedException(); } and it builds fine in visual studio 2017, but I get an error on my CI agent when using old msbuild ( v14.0 C:\Program Files (x86)\MSBuild\14.0\Bin\msbuid.exe consoleApplication.csproj. ): error CS1513: } expected . 回答1: You'll need to install msbuild-2015 on your CI agent. https://www.visualstudio.com/thank-you-downloading

What is the point of having async Main?

和自甴很熟 提交于 2019-11-29 06:13:09
As we know, C#7 allows to make Main() function asynchronous. What advantages it gives? For what purpose may you use async Main instead of a normal one? David Arno It's actually C# 7.1 that introduces async main. The purpose of it is for situations where you Main method calls one or more async methods directly. Prior to C# 7.1, you had to introduce a degree of ceremony to that main method, such as having to invoke those async methods via SomeAsyncMethiod().GetAwaiter().GetResult() . By being able to mark Main as async simplifies that ceremony, eg: static void Main(string[] args) => MainAsync

How can I enable all features of C# 7 in Visual Studio 2017 project?

女生的网名这么多〃 提交于 2019-11-29 04:23:16
问题 After Visual Studio 2017 was released I wanted to try to create simple console project with new C# 7 features. I expected that I simply download new Visual Studio 2017, then create new console project and can use new C# 7 features. But I can't. I can use some features, like Tuples if I install NuGet package System.ValueTuple. But for other features, I don't know what I need to do. For example this NuGet issue. Do I need to do all this dirty install now? Or I can enable c# 7 features in a more

C# 7 tuples and lambdas

烂漫一生 提交于 2019-11-29 04:22:49
问题 With new c# 7 tuple syntax, is it possible to specify a lambda with a tuple as parameter and use unpacked values inside the lambda? Example: var list = new List<(int,int)>(); normal way to use a tuple in lambda: list.Select(value => value.Item1*2 + value.Item2/2); i expected some new sugar to avoid .Item1 .Item2 , like: list.Select((x,y) => x*2 + y/2); The last line does not work because it is treated as two parameters for lambda. I am not sure if there is a way to do it actually. EDIT: I