Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Unity C# 8 support

Discussion in 'Experimental Scripting Previews' started by Jes28, Apr 18, 2019.

  1. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    559
    Simple question is: Do Unity have plans to support C# 8. If they Do - When? :)
     
    tigerleapgorge likes this.
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,414
    Yes, we do plan to support C# 8. We don't have a release date yet, although I expect our support for it will lag a bit behind Microsoft's.
     
    phobos2077, jGate99 and MechEthan like this.
  3. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,415
    I doubt C# 8 itself has a release date yet.
     
  4. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    559
    Ok it is good thanks :)

    Really I have just looking for undated roslyn compiler with support of C#8 so we can enable C#8 through csc.rsp and have benefits of NullableReference warnings in our project :)
     
  5. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,415
    You can play with the project I uploaded a while ago: Would the Roslyn Compiler compile C# 8.0 preview?

    It has Roslyn C# 8.0 Preview compiler integrated. If there is a version of Roslyn newer than 2.11.0.63519, I believe, you can just replace the whole Roslyn folder there with the new version.
     
    Mehrdad995 and SugoiDev like this.
  6. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    559
  7. Sir-Thorgeir

    Sir-Thorgeir

    Joined:
    Jan 17, 2013
    Posts:
    16
    Any update on the release date for C# 8 support?
     
  8. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    253
    Likely sometime in the 2020 release cycle.
     
  9. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,029
    stonstad likes this.
  10. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    559
    Cool.
    Waiting for Nullables support :)
    I have event prepare command to make nullables warnings to be errors :)
     
  11. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    88
    How's it looking with C# 8 in Unity, any chance it will come with 2019.3?
     
  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,414
    No, as mentioned above, it will be in the 2020 release cycle at the earliest. We don't currently have an ETA, so it might be later.
     
    TextusGames and jGate99 like this.
  13. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    559
    May be you have plans to update C# compiler to latest released so we can adlast turn on some syntactic sugar without true support of new features?
     
    holojotto, alexzzzz, Ramobo and 2 others like this.
  14. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    51
    vlads777 and OndrejP like this.
  15. Just_Max_

    Just_Max_

    Joined:
    Nov 13, 2019
    Posts:
    4
    +1, static extensions and default interface implementations are sweet ^_^
     
    OndrejP likes this.
  16. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    88
    +1
    I don't think static extensions are part of C# 8.
    But I'm waiting for default interface implementation and some minor things like readonly members and unmanaged constructed types might fit very well into DOTS.
     
  17. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    31
    +5 million
    simple 'using' statements :D
    switch expressions :cool:

    And a personal favorite;
    this:
    Code (CSharp):
    1. int[] array = { 1, 3, 5, 7, 9 };
    2. int i = array[^1]; // returns 9
    3. int[] subArray = array[1..3]; // returns { 3, 5 }
    4. int[] subArray2 = array[^2..]; // returns { 7, 9 }
     
    Last edited: Jan 5, 2020
    Rallix and TheSmokingGnu like this.
  18. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,415
    array[1..3] should return 2 elements.
     
  19. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,159
    More than c# 8 I need .net standard 2.1 support (fast span). is there a plan for that or will they potentially come together?
     
    Qbit86 and goncalo-vasconcelos like this.
  20. nsxdavid

    nsxdavid

    Joined:
    Apr 6, 2009
    Posts:
    397
    Fast span is pretty important for working with memory in cache friendly ways.
     
    goncalo-vasconcelos likes this.
  21. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,414
    We're looking at ways to bring C# 8 and .NET Standard 2.1 (or at least span support) to Unity. Our main concern is that .NET Standard 2.1 is not supported by .NET Framework, so adopting .NET Standard 2.1 would be a breaking change for the Unity ecosystem. In addition, .NET 5 is planned for release near the end of 2020, and we'd like to bring that to Unity users as well.

    We are working though the best way to handle this, but we don't have any updates yet.
     
  22. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    320
    I hope you guys can figure out a way for us to be able to just drop a new Roslyn whenever it comes out.
    With the old incremental compiler, we could just replace everything (except csc.exe) and it would work with the latest Roslyn from nuget. So, we could update the compiler with a simple nuget.exe install Microsoft.Net.Compilers

    Thanks for keeping us posted!
     
    Lorash, Tanner555, phobos2077 and 2 others like this.
  23. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,029
    I personally understand the 2.1 limitation, however having C# 8 support will be quite great as it will improve quality of life.
     
    Jorhoto likes this.
  24. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,159
    Fast Span only would be good enough for me.
     
    goncalo-vasconcelos and sirxeno like this.
  25. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,414
    Indeed C# 8 is separate from .NET Standard 2.1. The main difficulty with C# 8 is that it comes with new requirements for the runtime, namely default interface implementations. But it is possible to move Unity to C# 8 without a breaking change to the ecosystem.

    I think the biggest hurdle here is the C# 8 runtime requirements I mentioned just above. But when we get those implemented in the runtimes properly, this might be something that will work.
     
    Lorash, Tanner555, phobos2077 and 5 others like this.
  26. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    107
    Honestly I would be more excited if Unity moved from mono to .NET 5
     
    icave_user, JoNax97, Lorash and 6 others like this.
  27. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    31
    Whoops, you're right :eek:. Updated my answer immediately.
     
  28. WeirdBeardDev

    WeirdBeardDev

    Joined:
    Feb 16, 2013
    Posts:
    6
    C# 8 brings simplified null checks, no more
    Code (CSharp):
    1. if (x is null)
    2. {
    3.   x = expression;
    4. }
    instead
    Code (CSharp):
    1. x ??= expression;
     
  29. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    350
    That is good news.
    But will it comes with significant decrease of script compilation times?
     
  30. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    433
  31. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    158
    Span<T> support turns out to be pretty important for compat reasons. As the .NET ecosystem shifts to .NET Core for everything this increases library dependencies on Span<T>. For example, System.Text.Json is the standard platform serializer for .NET Core. It won't run on Unity because it has a dependency on Span<T>.
     
  32. Lorash

    Lorash

    Joined:
    May 6, 2019
    Posts:
    215
    MNNoxMortem, Peter77 and Jes28 like this.
  33. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    158
    @Lorash That's a good point. It's a widening fork that gets worse with time. ...Fast forward three years to the moment Unity announces an initiative to adopt .NET Core to address developer pain.

    In Unity's defense, it's truly hard to live on the bleeding edge, especially when that edge is sharp and owned by someone else (Microsoft). Know of any MS tech cancelled unceremoniously? When the change to .NET Core does happen it will be a strategically safe bet for Unity.
     
  34. Lorash

    Lorash

    Joined:
    May 6, 2019
    Posts:
    215
    Technically .NET is owned by the Foundation. Unity is a member of the Technical Steering Group so they have a reasonable amount of input to the future of .NET, I'd say it's very likely that their needs will be considered in future development - unlike the past situation where Mono was not owned by them at all, even partially, and .NET Framework was an entirely separate product.
     
    Jes28 and goncalo-vasconcelos like this.
  35. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    182
    Unity use a fork of the Mono runtime, this version is probably heavily personalized and deeply integrated in the core and make it difficult to replace.
     
  36. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    570
    After .NET 5 integrated into Unity, will it solve reload whole domain issue i.e. reload all the dlls including all the packages dll inside your project? I hope after this .NET 5 integrated, at least I just change one line of code it won't domain reload all the packages dll.
     
    Jes28 and kvfreedom like this.
  37. Lorash

    Lorash

    Joined:
    May 6, 2019
    Posts:
    215
    I wouldn't hope too much, on .NET Core you're limited to exactly one AppDomain and you can't unload it. We'll have to see how .NET 5 integrates Core and Framework, but I have a feeling it will lean towards Core of these two because that's the more modern one that runs on more platforms. Some kind of hot reload functionality like Unreal does could work better (although that one comes with its own heap of problems) and Unity has done the first steps with the new play mode options.
     
  38. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    142
    See AssemblyLoadContext. Also understand it, that's important too. Read this wonderful post as well. Additionally, CoreRT could eventually throw IL2CPP out the window. Why can't they just pause everything and move to .NET Core ASAP? That ought to make the .NET 5 update much less painful too. Oh, right, useless S*** like eye candy demos and visual scripting. F*** the actual programmers and editor performance, right? I wonder why Unity for AAA isn't really a well-established thing.
     
  39. Lorash

    Lorash

    Joined:
    May 6, 2019
    Posts:
    215
    I'm all for having Unity run on CoreRT, but at the same time I don't think it will immediately solve the domain reload issue when your code changes. For that they need to do separate work on the way things are serialized currently. To be very clear I'm not against this at all.

    Practically speaking my money is on when .NET 5 gets added into Unity it will be mostly as a direct Mono replacement (one of 5's stated goals is to be a viable drop-in Mono replacement), which will obviously come with immediate benefits such as the generational and compacting GC (that Unity is preparing for already) and better codegen.
     
  40. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    142
    Hmm... To be fair, I'm starting to question my understanding of domain reload and how `AssemblyLoadContext` would solve it... In any case, there's still the performance benefits of .NET Core. Even if that alone doesn't solve domain reload (it won't), it should still speed it up a fair bit.

    From what I understand, they will have you choose which runtime to use instead of just merging all of them.
     
    Lorash likes this.
  41. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    51
    Any update on c#8 availability? Maybe in beta-preview version?

    Thanks ;)
     
    Qbit86 likes this.
  42. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    4,414
    We don't have any update at the moment, sorry!
     
  43. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    51
    I am starting a new Unity project and it is going to be based on DOTS.

    From my researchs, DOTS are all about using structs instead of classes, however, structs cannot inherit from classes or even from other structs, so they are limited to interfaces. Without default interface implementation (available after c#8) I can see a big mess and multiple repetitions of code.

    Having this feature seems to me obvious for a proper DOTS implementation, but maybe I'm wrong and there is a workaround.

    Thanks!
     
    sand_lantern and TextusGames like this.
  44. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    433
    Not so obvious for me. Are you going to access instances of your value types via references to the interfaces? Then you'll end up with a lot of allocations for boxed objects scattered throughout the managed heap — avoiding of which is the main (the only?) point of DOTS.
     
  45. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    182
    If you need a lot of inheritance, you think too much of OOP.
    As exemple, if you have some component data that must have the same base date, do not inherit from a base, but split the shared properties to an separate component.

    Default interface implementation work only for methods and you can use extensions on interfaces to share functionality.
     
  46. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    320
    I'm looking forward to C#8 for a few reasons (like using declarations and static local functions).
    But, the most important thing for me is for us to have choice. Remember when we were stuck with obsolete compilers (and runtimes) and had to resort to all sorts of hackery? I don't want to have us moving in that direction ever again.

    The current compiler is nice, but much nicer would it be for us to be able to use nuget to install Microsoft.Net.Compilers and we would have the latest compiler around.

    I'm hopeful because Unity devs already replied to this thread positively and it seems the reason C#8 isn't here yet is due to runtime changes.

    Happy to see more developers voicing their opinion here. Let's hope this can be used by the many dedicated Unity devs to justify getting extra resources to bring this feature to life.
     
    Jes28 likes this.
  47. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    158
    Not going to start a debate, but I like OOP and it works for me. I hear the siren song of Unreal Engine calling for me...
     
  48. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    182
    I start the debate, an i would argue pure DOTS can be faster then Unreal with C++/OOP, but it's too early to compare them. :D

    DOTS: Data-Oriented
    OOP: Object-Oriented

    These are two different program designs, force one into the other is not a good idea.
    But i think, you can mix OOP and DOTS in a project depending on the task. The systems in ECS see for me like OOP.
     
    sand_lantern likes this.
  49. Lorash

    Lorash

    Joined:
    May 6, 2019
    Posts:
    215
    .NET is very serious about backwards and forwards compatibility, way back to .NET 1.0. You can use a newer version of C# with an older runtime, you'll just miss out on certain features. For instance in a project that I worked on we were stuck on Framework 4.0 for a long time but that didn't stop us from upgrading to C# 5 and then 6, just with the knowledge that, e.g.,
    await
    would not work.
     
  50. Lorash

    Lorash

    Joined:
    May 6, 2019
    Posts:
    215
    Never forget the first rule of optimization: don't do it.
     
unityunity