C# 7.3 support?
c# 7.3 added equality support for tuples (== and !=) and while the following code works in Visual Studio 15.7 Preview, its causes a compilation error in linqpad 5.31
`(1,1) == (1,2)`
`CS0019 Operator '==' cannot be applied to operands of type '(int, int)' and '(int, int)'`
Comments
-
-
This sample statement does not appear to work although LinqPad states 7.3 support:
var value = 2; var result= value switch { 1 => "Case 1", 2 => "Case 2", 3 => "Case 3", 4 => "Case 4", };
results in:
CS1002 ; expected
CS1003 Syntax error, '(' expected
CS1525 Invalid expression term '{'
CS1026 ) expected
I am sorry to be out of it, but these really simple examples should work, yes? Or do I need to set an option somewhere?
What I really want to do is this:
Replace all these if statements :
** if (e.Argument.StartsWith(@"DISCARD:INSPECTION")) { DiscardInspection(); } if (e.Argument == @"ERROR") { Response.Redirect("~/Error.aspx"); } if (e.Argument == @"EXPIRATION") { Log?.Debug("EditFormCS redirects to Expiration"); Response.Redirect("~/Expiration.aspx"); }**
with :
var action = e.Argument switch { e.ArgumentStartsWith(@"DISCARD:INSPECTION")) => DiscardInspection(), e.Argument == @"ERROR" => Response.Redirect("~/Error.aspx"), e.Argument == @"EXPIRATION" => { Log?.Debug("EditFormCS redirects to Expiration"); Response.Redirect("~/Expiration.aspx"); }, ... ... ... ..
}
and then invoke the action...
Hopeless?
Thank you.
-
Works ok with LinqPad 6 beta, so I'm guessing you are using LinqPad 5.
-
Switch expressions were introduced in C# 8, which requires .NET Core (and hence LINQPad 6).
-
This is a few years old, but I am copying code directly from my .NetFrameWork (3.7.2) code into LinqPad 5, but I get an error for Invalid Expression Term in a Switch Statement. however, I get no error in VS and the code works. Am I missing a setting change in LinqPad5?
switch (phonenumber.Length) { case > 6: sb.Append($"{phonenumber.Substring(0, 3)}-{phonenumber.Substring(3, 4)}"); break; case <= 3: sb.Append(phonenumber); break; default: sb.Append($"{phonenumber.Substring(0, 3)}-{phonenumber.Substring(3, phonenumber.Length - 3)}"); break; }
-
The feature you are using (relational patterns) is part of C# 9. LINQPad 5 only supports up to C# 7.3 or 8 with experimental Roslyn features enabled. Are you using C# 9 but targeting .Net Framework 3.7.2?
-
We are running a .Net 4.7.2, and we can not set the target language version, it is disabled and automatically set by the framework.
Again, this has been in our code for a while. (FYI, I mean 4.7.2 (type-o on my part..sorry)
-
so, our projects are setup to use "LangVersion" of 9.