c#-7.0

'ValueTuple<T1, T2>' exists in both 'System.ValueTuple …' and 'mscorlib …'

半世苍凉 提交于 2019-12-23 10:59:26
问题 B"H I am trying to use the new Tuple features of C# 7, and running into a little bit of an issue. Actually, they were working fine, and I am not sure what changed to make them break. I am working with an ASP.Net 4 , MVC 5 , targeting .net framework 4.6.1 So in order to use tuples I had to a the Nuget Package 'System.ValueTuple' . Without it, the project won't compile. It worked fine for a while. Then today when I load any page that uses Tuples I get Compiler Error Message: CS0433: The type

Why can I ref return an item of an array that only exists inside the method?

百般思念 提交于 2019-12-21 10:18:07
问题 I was trying out the new ref returns of C#7. I am able to compile and build this: public ref string MisUseRefReturn(int index) { string[] array = { "a", "b", "c", "d" }; return ref array[index]; //array[2] gets out of scope when this method returns! } According to MSDN: The return value cannot be a local variable in the method that returns it; it must have a scope that is outside the method that returns it. It can be an instance or static field of a class, or it can be an argument passed to

C# 7 Pattern Match with a tuple

安稳与你 提交于 2019-12-21 07:41:41
问题 Is it possible to use tuples with pattern matching in switch statements using c# 7 like so: switch (parameter) { case ((object, object)) tObj when tObj.Item1 == "ABC": break; } I get an error that says tObj does not exist in the current context . I have tried this as well: switch (parameter) { case (object, object) tObj when tObj.Item1 == "ABC": break; } This works fine: switch (parameter) { case MachineModel model when model.Id == "123": break; } 回答1: Remember that C#7 tuples are just

C# 7 Pattern Match with a tuple

痴心易碎 提交于 2019-12-21 07:41:15
问题 Is it possible to use tuples with pattern matching in switch statements using c# 7 like so: switch (parameter) { case ((object, object)) tObj when tObj.Item1 == "ABC": break; } I get an error that says tObj does not exist in the current context . I have tried this as well: switch (parameter) { case (object, object) tObj when tObj.Item1 == "ABC": break; } This works fine: switch (parameter) { case MachineModel model when model.Id == "123": break; } 回答1: Remember that C#7 tuples are just

Ref returns restrictions in C# 7.0

假装没事ソ 提交于 2019-12-21 03:25:14
问题 I am trying to understand the following excerpt from an official blog post about new features in C# 7.0 concerned with ref returns. You can only return refs that are “safe to return”: Ones that were passed to you, and ones that point into fields in objects. Ref locals are initialized to a certain storage location, and cannot be mutated to point to another. Unfortunately, the blog post does not give any code example. Would greatly appreciate it if someone could shed more light into the

How to declare a C# Record Type?

断了今生、忘了曾经 提交于 2019-12-21 03:13:09
问题 I read on a blog that C# 7 will feature record types class studentInfo(string StudentFName, string StudentMName, string StudentLName); However when I tried it, I get these errors CS0116 A namespace cannot directly contain members such as fields or methods CS1022 Type or namespace definition, or end-of-file expected CS1514 { expected How is this supposed to work? 回答1: Record types are not (yet) implemented in C#. See the proposal in the official GitHub repository: https://github.com/dotnet

Building msbuild 15 project programmatically

非 Y 不嫁゛ 提交于 2019-12-20 18:10:10
问题 I'm trying to build a simple C# 7 class library project created with VS2017. MSBuild from framework assemblies is outdated, so I'm referencing Microsoft.Build , Microsoft.Build.Engine and Microsoft.Build.Framework from MSBuild folder within visual studio ( C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin ). Still, when I do this: using (var collection = new ProjectCollection()) { var proj = collection.LoadProject(@"c:\projects\Sample\Sample.csproj"); // <--

In C# can you define an alias to a value tuple with names?

老子叫甜甜 提交于 2019-12-20 09:07:55
问题 I know it's possible to define aliases in C# with the using keyword. e.g. using ResponseKey = System.ValueTuple<System.Guid, string, string>; However, is it possible to define one using the new syntax for value tuples? using ResponseKey = (Guid venueId, string contentId, string answer); This syntax does not appear to work. Should it? 回答1: This has been requested, and recorded in the Roslyn repo on Github. However it received a mixed reception there, and the proposed record types, would more

C# 7.0 'out variables' in Visual Studio 2015 [duplicate]

家住魔仙堡 提交于 2019-12-20 05:39:22
问题 This question already has answers here : How to use C# 7 with Visual Studio 2015? (2 answers) Closed 2 years ago . I would like to compile C# 7.0 solution in Visual Studio 2015, but I have syntax error: Error ErrorMessage : 'DateTime' is a type but is used like a variable. This is related to 'out variables' changed in C# 7.0 Unfortunately I can not use VS2017 (community) and THIS TUTORIAL - not working (master branch is adapted to VS2017 now). Do you have an idea how to solve this problem?

Overloading in local methods and lambda

徘徊边缘 提交于 2019-12-19 17:04:55
问题 static void Main() { void TestLocal() => TestLocal("arg"); TestLocal("arg"); } static void TestLocal(string argument) { } In this example, local function has the same name as another method, which I want to overload. But this method is invisible from inside of this function, and even from inside of Main() . The same is for lambdas: static void Main() { Action TestLambda = () => TestLambda("arg"); } static void TestLambda(string argument) { } I understand, why lambda hides outer method -