c#-7.0

What's the benefit of var patterns in C#7?

≯℡__Kan透↙ 提交于 2019-11-30 12:31:20
问题 I don't understand the use case of var patterns in C#7. MSDN: A pattern match with the var pattern always succeeds. Its syntax is expr is var varname where the value of expr is always assigned to a local variable named varname . varname is a static variable of the same type as expr . The example on MSDN is pretty useless in my opinion, especially because the if is redundant: object[] items = { new Book("The Tempest"), new Person("John") }; foreach (var item in items) { if (item is var obj)

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

℡╲_俬逩灬. 提交于 2019-11-30 12:23:46
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . A team mate told me that in C# 7.0 it's is possibile to return multiple values from a function natively. Can anybody provide some example? Thanks 回答1: What do you mean by natively ? Actualy C# 7 has a very useful new feature that lets you return more than one value from a method thanks to tuple

When to use: Tuple vs Class c# 7.0

血红的双手。 提交于 2019-11-30 11:15:45
问题 Before Tuples, I used to create a class and its variables then create object from this class and make that object the return type for some functions. Now with the tuples I can do the same and in c# 7.0 we can assign understandable names for tuples properties (before this it was item1 , item2 , etc..) So now I am wondering, when should I use tuples and when should I create a class in c# 7.0? 回答1: As this answer is causing some confusion amongst some folk here, I should clarify that - as per

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

♀尐吖头ヾ 提交于 2019-11-30 08:56:06
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 default, but I'm not able to get this to work. Even taking the exact ArbitraryAsyncReturns.zip

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

点点圈 提交于 2019-11-30 06:39:04
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 . You'll need to install msbuild-2015 on your CI agent. https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15 The default install directory for msbuild will be: C:\Program Files (x86

How to create a List of ValueTuple?

谁都会走 提交于 2019-11-30 06:35:55
Is it possible to create a list of ValueTuple in C# 7? like this: List<(int example, string descrpt)> Method() { return Something; } You are looking for a syntax like this: List<(int, string)> list = new List<(int, string)>(); list.Add((3, "first")); list.Add((6, "second")); You can use like that in your case: List<(int, string)> Method() => new List<(int, string)> { (3, "first"), (6, "second") }; You can also name the values before returning: List<(int Foo, string Bar)> Method() => ... And you can receive the values while (re)naming them: List<(int MyInteger, string MyString)> result = Method

C# 7 tuples and lambdas

瘦欲@ 提交于 2019-11-30 05:34:12
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 tried double parentesis in lambda definition and it didn't work: ((x,y)) => ... , and maybe it was stupid

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

家住魔仙堡 提交于 2019-11-30 05:34:09
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 simple way? Scott Chamberlain For arbitrary task-like types you linked to in the 2nd part of your

What's the benefit of var patterns in C#7?

冷暖自知 提交于 2019-11-30 02:56:52
I don't understand the use case of var patterns in C#7. MSDN : A pattern match with the var pattern always succeeds. Its syntax is expr is var varname where the value of expr is always assigned to a local variable named varname . varname is a static variable of the same type as expr . The example on MSDN is pretty useless in my opinion, especially because the if is redundant: object[] items = { new Book("The Tempest"), new Person("John") }; foreach (var item in items) { if (item is var obj) Console.WriteLine($"Type: {obj.GetType().Name}, Value: {obj}"); } Here i don't see any benefits, you

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

岁酱吖の 提交于 2019-11-30 02:38:04
A team mate told me that in C# 7.0 it's is possibile to return multiple values from a function natively. Can anybody provide some example? Thanks What do you mean by natively ? Actualy C# 7 has a very useful new feature that lets you return more than one value from a method thanks to tuple types and tuple literals . Consider the following function: (string, string, string) MyCoolFunction() // tuple return type { //... return (firstValue, secondValue, thirdValue); } Which can be used like this: var values = MyCoolFunction(); var firstValue = values.Item1; var secondValue = values.Item2; var