c#-7.0

Compile errors when using C# 7 features in new VS Studio 2017 ASP.NET MVC 5.2.3 project

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 05:28:17
I'm using VS 2017 usually for console applications. But now I have to create a new ASP.NET MVC project and found something curious. Auto generation of getter and setter (context menu "Quick actions and refactoring < Ecapsulate field (and use property)") raise a compiling error. To give an easy example: public class Person { private string firstname; public string Firstname { get => firstname; set => firstname = value; } } Error message: 1>------ Build started: Project: DummyASPNETMVC, Configuration: Debug Any CPU ------ 1>C:\dev\C#\DummyASPNETMVC\DummyASPNETMVC\Models\Person.cs(12,39,12,41):

Compile errors when using C# 7 features in new VS Studio 2017 ASP.NET MVC 5.2.3 project

二次信任 提交于 2019-12-01 04:18:19
问题 I'm using VS 2017 usually for console applications. But now I have to create a new ASP.NET MVC project and found something curious. Auto generation of getter and setter (context menu "Quick actions and refactoring < Ecapsulate field (and use property)") raise a compiling error. To give an easy example: public class Person { private string firstname; public string Firstname { get => firstname; set => firstname = value; } } Error message: 1>------ Build started: Project: DummyASPNETMVC,

Can a C# named Tuple be used as an MVC page model type?

人走茶凉 提交于 2019-12-01 04:15:05
In C# 7 you can have named tuples: var foo = (Name: "Joe", Age: 42); If I pass this to a MVC model using: return View(foo); Then what syntax should be used in the cshtml file to declare the model? Although this doesn't work, something like... @model (string Name, int Age); As for current time you can't and need to use @model Tuple<string, int> //or @model ValueTuple<string, int> For the difference between the two options: What's the difference between System.ValueTuple and System.Tuple? You can follow on GitHub: Razor throws CompilationFailedException when iterating over named Tuple (I know it

Discard feature significance in C# 7.0?

余生颓废 提交于 2019-12-01 03:43:25
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)) The discards are basically a way to intentionally ignore local variables which are irrelevant for the purposes of the code being produced. It's like when you call a method that

Name ValueTuple properties when creating with new

萝らか妹 提交于 2019-12-01 02:52:16
I know I can name parameters when I create a tuple implicitly like: var me = (age: 21, favoriteFood: "Custard"); Is it possible to name parameters when a tuple is created explicitly? i.e. var me = new ValueTuple<int, string>(21, "Custard"); No, you can't. The ValueTuple types are actually independent of the named field support in C#. The latter works more like named properties for anonymous types. That is, the compiler analyzes the code and generates aliases to the appropriate members according to your declarations and usages. It is through the assignment that the compiler learns the names of

C# 7:How can I deconstruct an object into a single value using a tuple?

不羁的心 提交于 2019-12-01 01:15:30
问题 One of the nice new features of C# 7 is the possibility to define deconstructors for classes and assign the deconstructed values directly to a value tuple. However, in the case that the object is deconstructed into a single value, I can't find a way to assign it to a tuple. Although there is a type for tuples with a single element ( ValueTuple<T> ), the shorthand syntax using parentheses doesn't work here. The only way I found to access the deconstructor was to call the Deconstruct method

Assigning local functions to delegates

前提是你 提交于 2019-11-30 17:41:57
In C# 7.0 you can declare local functions, i.e. functions living inside another method. These local functions can access local variables of the surrounding method. Since the local variables exist only while a method is being called, I wondered whether a local function could be assigned to a delegate (which can live longer than this method call). public static Func<int,int> AssignLocalFunctionToDelegate() { int factor; // Local function int Triple(int x) => factor * x; factor = 3; return Triple; } public static void CallTriple() { var func = AssignLocalFunctionToDelegate(); int result = func(10

Does C# 7 have array/enumerable destructuring?

故事扮演 提交于 2019-11-30 16:49:51
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 another IEnumerable. I have a solution, but feel that destructuring will look cleaner. If you want a

Assigning local functions to delegates

a 夏天 提交于 2019-11-30 16:43:00
问题 In C# 7.0 you can declare local functions, i.e. functions living inside another method. These local functions can access local variables of the surrounding method. Since the local variables exist only while a method is being called, I wondered whether a local function could be assigned to a delegate (which can live longer than this method call). public static Func<int,int> AssignLocalFunctionToDelegate() { int factor; // Local function int Triple(int x) => factor * x; factor = 3; return

HttpRequestMessageExtensions not being found at run-time in Azure Function

喜夏-厌秋 提交于 2019-11-30 12:37:24
I've got an Azure Function app that creates a precompiled DLL (so it uses normal .cs files, not the older .csx method, pre-VS2017). Previously, it was targeting .Net Framework 4.5.2. I updated it to 4.7 so as to use some of the new C# 7 features. I updated my NuGet packages by doing "Update-Package -Reinstall" and verified that they all have the "net47" target set in my packages.config file. Everything compiles fine. But when I call a function that uses either of 2 HttpRequestMessageExtensions methods, I get an exception. One example of the exception is this: Method not found: 'System.Net.Http