Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

C# Compiler Upgraded C# Compiler on 5.3.5p8

Discussion in 'Experimental Scripting Previews' started by joncham, Jul 21, 2016.

Thread Status:
Not open for further replies.
  1. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    276
    Hello All!

    Here is a build matching our 5.3.5p8 patch release with the upgraded C# compiler included:

    http://bit.ly/29Vvn7W


    Please backup your project before trying this build.

    Please post any issues you have to this forum.

    The upgraded Mono C# compiler (corresponding to Mono 4.4) will be used to compile all your C# scripts.

    Important Notes
    • The Mono runtime we use in our Editor and Players has not been upgraded.

    • The compiler still targets the C# 4 language. This is approximately what our old compiler supported.

    • The compiler still target the .Net 3.5 framework profile we previously supported.

    This is the first step towards the mythical "Mono/.Net Upgrade".


    FAQ


    How will this affect me?

    This upgrade should “just work”; it does not expose any new features. It should not affect how you work today, other than a few benefits. The new Mono and C# compiler generally runs faster and has many years of bug fixes compared to our previous compiler.

    What should I be testing/looking for?

    In theory it could cause weird behavior at runtime. However, we have not seen that in any of our testing.

    Most likely you will see an issue at compile time. So far, we have seen this if:

    - you have invalid C# that the old compiler allowed and the new compiler properly errors on
    - your code or a plugin assumes something specific about the compiler (install location, name, etc) and errors

    One more thing, if you regularly debug C# using MonoDevelop or VSTU it would be good to hear any issues you encounter. The compiler output should be compatible, but the debugger may be sensitive to some generated code patterns.

    Are there any breaking changes?

    Yes, there is one known breaking change. The new compiler has a change to how a closure captures the foreach variable. This was a breaking change the C# language team made. This means that the following snippet of code will now print "b a r" rather than the current "r r r".

    Code (csharp):
    1.  
    2. var actions = new Action[3];
    3.  
    4. var j = 0;
    5.  
    6.  
    7. foreach (var c in "bar")
    8.  
    9.     actions[j++] = () => Console.WriteLine(c);
    10.  
    11.  
    12. foreach (var a in actions)
    13.  
    14.     a();
    15.  
    This is the behavior you would get using a modern version of VS or Mono, and is the behavior our current WSA platform exhibits.


    Details here: https://msdn.microsoft.com/en-us/library/hh678682(v=vs.110).aspx


    Does this fix the 'foreach' allocates issue?

    Yes, under certain circumstances. See an explanation of the issue http://stackoverflow.com/questions/18552669/memory-allocation-when-using-foreach-loops-in-c-sharp.


    tl;dr; this generally means that doing 'foreach' over an Array, List<T>, Dictionary<T>, or HashSet<T> should not allocate.


    Why don't we target C# 5 or C# 6?

    As a first step, we are just rolling out the new compiler. We may enable newer C# features in the future.


    A few issues with supporting newer C#

    • Our ecosystem does not completely handle newer C# yet (script updater, debugger, certain MonoDevelop/VisualStudio versions)

    • We are still targeting the old 3.5 .Net profile. It's somewhat confusing to support newer C# while still targeting an older framework. Writing code from scratch may work, but any existing code probably needs both new C# and new framework.
     
    Last edited by a moderator: Jul 22, 2016
Thread Status:
Not open for further replies.