- Home /
Are switch -expressions- compatible with Unity?
I'm on a Macbook and I'm using Visual Studio for coding, I usually take the recommendations that Visual Studio gives me when I'm coding, such as shortcuts or expressions that makes me code less and more efficient... So I was making code and in a point I had to use a switch to give a variable a different value depending on other variable, so when I finished, the IDE recommended me to replace the switch statement with the switch expression to short the code, which it looks something like this:
var result = bank.Status switch
{
BankBranchStatus.Open => true,
BankBranchStatus.Closed => false,
BankBranchStatus.VIPCustomersOnly => isVip
};
However when I saved it, Unity complained like if it didn't recognize the code, so I return it back to normal, but gives me the question: Is this expression new to C#? and if so, will Unity be compatible with that on later versions?
I don't think the IDE is doing any analysis. It's only suggesting whatever that monstrosity since it's newer.
Answer by TreyH · Dec 11, 2019 at 10:00 PM
C# 8 (and that syntax) aren't supported yet. You'll need to use the more verbose syntax until then.
Damn it, haha, thanks for the info, I hope Unity guys make that interesting expressions compatible soon...
I wouldn't wait for it. We were stuck on C# 3.5 / 4 for about 8 years. Just recently Unity made the step up to C# 7. C# 8 as a whole is a relatively new language specification anyways (about september 2019). So just set your VS to C# 7 or lower to avoid any further issues.
Your answer
Follow this Question
Related Questions
Why are Vector2 parameters of methods , set to be = new Vector2 , read as being null by dll ? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
VS Code issues when used with a Unity project 1 Answer
System.Net.WebSockets Namespace not available in Unity generated Project 2 Answers