Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by israelg99 · Nov 20, 2015 at 01:38 PM · c#unity 5unity5propertiessupport

C# - Doesn't Unity support Expression Bodied Properties?

I have this line of code in C#.

 private static float Width => 0.015f;

Unity is complaining about the following:

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

Doesn't Unity support Expression Bodied Properties?
It's a new amazing feature of C# 6.0

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nereacastellanos · Jun 05, 2018 at 05:02 AM 0
Share

@Cameron860 @Bunny82 I have this line of code:


public static float4x4 identity => new float4x4 { m0 = new float4(1.0f, 0.0f, 0.0f, 0.0f), m1 = new float4(0.0f, 1.0f, 0.0f, 0.0f), m2 = new float4(0.0f, 0.0f, 1.0f, 0.0f), m3 = new float4(0.0f, 0.0f, 0.0f, 1.0f) };


For what I should to change this?

$$anonymous$$y error is "Feature `expression bodied members' cannot be used because it is not part of the C# 4.0 language specification´

Thanks!

avatar image Bunny83 nereacastellanos · Jun 05, 2018 at 10:14 AM 0
Share

Just create a normal property:

 public static float4x4 identity {
     get {
         return new float4x4 {
             m0 = new float4(1.0f, 0.0f, 0.0f, 0.0f),
             m1 = new float4(0.0f, 1.0f, 0.0f, 0.0f),
             m2 = new float4(0.0f, 0.0f, 1.0f, 0.0f),
             m3 = new float4(0.0f, 0.0f, 0.0f, 1.0f) };
     }
 }



3 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by Thaun_ · Nov 10, 2017 at 11:21 AM

You can now change framework: (Project Settings > Player > Other Settings) alt text


skjermbilde.png (4.0 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Cameron_SM · Jul 09, 2016 at 04:13 PM

@israelg99 I feel your pain. Sadly, there's not a lot you can do about it unless you can write a lot of your code on a seperate library outside of Unity.

Many of the new C# 6.0 features can be used with .NET 3.5 framework target code because they're just syntactic sugar that lets you type less. The compiled CLR (Common Language Runtime) bytecode is identical. Alas, Roslyn (which is what Microsoft named their new C# 6.0 compiler) is only currently being used in Visual Studio. The .NET Framework version (2.0, 3.5, 4.0 etc) and the C# version aren't the same thing.

If you write your code as a seperately compiled DLL and use Visual Studio you can use expression bodies and all the other C# 6.0 features (those supported in the .NET 3.5 target at least) with Unity because it's all compiled to valid .NET 3.5 Framework bytecode inside the DLL and Unity won't complain. However, Unity's built in automatic compiler is old and won't understand the newer syntax. I hope they update it , Microsoft already have Roslyn running on a Mac with Visual Sdutio Code.

I think the only .NET 3.5 features that unity doesn't support are largly based around IL injection, code emit and dynamic compilation (Expression classes etc).

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 09, 2016 at 06:17 PM 3
Share

I generally agree that some newer features would be quite nice to have in Unity, however those expression properties aren't one of them. It doesn't save much and just makes it harder to convert it into a full property if needed. They can only be used for read-only properties. They also can be overlooked easily.

This does the same thing:

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

as this:

 private static float Width => 0.015f;

In this particular case it would be easier and better for performance to use a simple constant as it's not always given that simple properties are always inlined.

 private const float Width = 0.015f;

If the "property" should be an instance member you can declare a field as "readonly":

 private readonly float Width = 0.015f;

So i wouldn't call those expression properties an amazing feature (as the OP did). The lambda operator itself can make code shorter and easier to read, however the declaration of properties are "standalone" code parts and not inside a long code fragment.

As soon as your "expression" gets more complex you would need some line breaks anyways.

avatar image Fattie Bunny83 · Mar 26, 2017 at 05:59 PM 0
Share

thanks for that handy tip, Bunny83 !

avatar image Cameron_SM · Jul 12, 2016 at 03:21 AM 0
Share

@Bunny82 I think the OP was just showing a $$anonymous$$imal case. I'm generally in favor of less scaffolding if possible (with the exception of if/else brackets, ommiting those is evil I$$anonymous$$O).

I think simplifying the creation of tiny immutable methods/properties that do one thing and do it well was a really good move towards making coders adopt more immutable practices which sadly, most ignore because it takes longer to impliment. Now, not so much.

 public string FirstName { get; private set; }
 public string LastName { get; private set; }
 public string FullName => $"{FirstName} {LastName}";
 public string FormalName => $"{FirstName[0]}. {LastName}";

I'm also fairly certain every c# compiler will inline simple getters. As for suggesting it be a constant, that is a behavioural change - the author may intend the property to represent state and hence not want it to be immutable.

avatar image
0

Answer by gjf · Nov 20, 2015 at 01:38 PM

no, it doesn't. not sure what the latest version of c# used is (check the docs), but it's definitely not 6.0.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Nov 20, 2015 at 01:41 PM 0
Share

It's an old version of $$anonymous$$ono, kind of a mixture between .NET 2.0 and 3.5 if my understanding/memory is correct.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object rotation snaps back to 0° 1 Answer

"on" and "off" the effect. 0 Answers

Must I attach every script to a gameobject in order to work ? 2 Answers

How to define a slider on script file? 1 Answer

A* pathfinding generating new path when created new obstacle 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges