c#-7.0

Pattern match variable scope

自闭症网瘾萝莉.ら 提交于 2019-12-19 13:49:10
问题 In the Roslyn Pattern Matching spec it states that: The scope of a pattern variable is as follows: If the pattern appears in the condition of an if statement, its scope is the condition and controlled statement of the if statement, but not its else clause. However the latest Microsoft "What's new" posts and presentations are showing this example: public void PrintStars(object o) { if (o is null) return; // constant pattern "null" if (!(o is int i)) return; // type pattern "int i" WriteLine

How do I enable C# 7 builds in Team Foundation Server 2015?

限于喜欢 提交于 2019-12-19 07:49:29
问题 We have Team Foundation Server (TFS) 2015 installed on-premises. We would like to use Visual Studio 2017 to take advantage of the latest C# language features. We are not ready to upgrade to TFS 2017. What are the steps required to allow the latest C# language features to build in TFS 2015? 回答1: I didn't follow these steps exactly, but based on what I have learned, I think they would have worked: Install Visual Studio 2017 Build Tools on the XAML Build Configuration server Add these lines to

C# 7 features don't work within a web project on Visual Studio 2017 RC

Deadly 提交于 2019-12-19 05:18:46
问题 I have several projects in the solution, and the C# 7 features, such as tuples and throw expressions, work fine in all of the library projects, but there is a (non Core) web project that doesn't compile due to errors on the C# 7 features. Right after compiling, the error window quickly clears itself, presumably because the IDE/editor compiles the same units without error. I have to use the output window to see the compiler errors. It is as though the IDE/editor are assuming C# 7, but the

Discard feature significance in C# 7.0?

岁酱吖の 提交于 2019-12-19 05:17:14
问题 While going through new C# 7.0 features, I stuck up with discard feature. It says: Discards are local variables which you can assign but cannot read from. i.e. they are “write-only” local variables. and, then, an example follows: if (bool.TryParse("TRUE", out bool _)) What is real use case when this will be beneficial? I mean what if I would have defined it in normal way, say: if (bool.TryParse("TRUE", out bool isOK)) 回答1: The discards are basically a way to intentionally ignore local

Why doesn't returning by ref work for elements of collections?

一世执手 提交于 2019-12-19 05:13:08
问题 The following example of returning by reference is from What’s New in C# 7.0: public ref int Find(int number, int[] numbers) { for (int i = 0; i < numbers.Length; i++) { if (numbers[i] == number) { return ref numbers[i]; // return the storage location, not the value } } throw new IndexOutOfRangeException($"{nameof(number)} not found"); } That compiles without any problems (as you'd expect as it's copied from the Microsoft blog). I've written this one: private static ref int GetReference

Inline variable declaration doesn't compile when using '== false' instead of negation operator

最后都变了- 提交于 2019-12-18 13:07:51
问题 Consider the following snippets: void Foo(object sender, EventArgs e) { if (!(sender is ComboBox comboBox)) return; comboBox.DropDownWidth = 100; } compared to void Bar(object sender, EventArgs e) { if ((sender is ComboBox comboBox) == false) return; comboBox.DropDownWidth = 100; } Code including Foo() successfully compiles in .Net 4.6.1, while code including Bar() results in Use of unassigned local variable 'comboBox' . Without getting into a debate over the reasons behind using == false

How to create a List of ValueTuple?

女生的网名这么多〃 提交于 2019-12-18 12:09:45
问题 Is it possible to create a list of ValueTuple in C# 7? like this: List<(int example, string descrpt)> Method() { return Something; } 回答1: 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() => ...

Expression bodied get / set accessors feature in c# 7.0

旧巷老猫 提交于 2019-12-18 07:36:33
问题 I'm having this code in a class private string test; public string Test { get => test; set => test = value; } But the compiler won't let me compile. It says CS1043 { or ; expected CS1513 } expected I'm using VS 2017 and targeting .NET FW 4.6 with a MVC 5 Project Any idea why it isn't working? 回答1: You have to set the compiler to version 7 in your project. Project properties → (tab) Build → Advanced → Language version = C# 7.0 UPDATE BY @gsharp check also your (NuGet) reference to the .NET

What is the point of having async Main?

杀马特。学长 韩版系。学妹 提交于 2019-12-18 04:39:23
问题 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? 回答1: 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

Inline variable declaration not compiling

白昼怎懂夜的黑 提交于 2019-12-17 23:39:00
问题 I've been getting a message in Visual Studio 2017, specifically, IDE0018 Variable declaration can be inlined. So I try using an inline variable declaration the way it's mentioned in the visual studio 2017 release notes, but I can't get my project to compile. It show no error messages, but the output shows " Rebuild All failed..... error CS1525: Invalid expression term 'int' " The error only shows up in the output, not as an actual error in the error list. Here is an actual example of the code