0
\$\begingroup\$

I wrote the following line in Unity5 using C#:

private static float Width => 0.015f;

Unity complained about the following issue:

Unexpected symbol `=>' in class, struct, or interface member declaration

Doesn't Unity5 support Expression Bodied Properties(a new feature in C# 6.0)?

I am really lost here...

\$\endgroup\$
1
\$\begingroup\$

No, Unity doesn't support expression bodied members.

You'll find for most things, Unity is not at the cutting edge of C# / .NET features. That's because the version of Mono it uses under the hood to provide the Common Language Runtime across multiple platforms only handles up to about .NET 2.0-3.5 (circa 2005-2007):

Screenshot from build settings

This restriction also applies to using IL2CPP for iOS and WebGL builds, since it's still going through the same process to generate the intermediate language.

So, don't expect to use any .NET features introduced in the past decade, at least not yet. The Unity developers have announced that they intend to upgrade to more recent versions of Mono to allow the use of newer .NET features as Unity continues to evolve.

Fortunately, expression bodied properties are just syntactic sugar, and you can get the same compiled result today with only slightly more verbose source:

private static float Width { get { return 0.015f; } }

Edit: User Jaakko Lipsanen posted an answer to a related question which linked to a project to make many C# 6.0 features available for Unity through Visual Studio. It still compiles to ~.NET 3.5 IL under the hood, but supports C# 6.0 source code. I haven't worked with it myself yet, so I can't attest to how well it works, but if you prefer working in 6.0 it may be worth a look.

\$\endgroup\$
2
  • \$\begingroup\$ That's exactly what I did meanwhile, even though I adore that syntactic sugar, the compiler eventually generates the same results, thank you for answering though! \$\endgroup\$ – julian Nov 20 '15 at 13:49
  • \$\begingroup\$ @Israelg99 I came across another answer which might be useful to you in getting C# 6.0 support in Unity - see my edit above. \$\endgroup\$ – DMGregory Jan 3 '16 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.