Search Unity

  1. We want you to join us at GDC this year! We've added new sessions to Unity Central, space is limited so sign up now!
    Dismiss Notice
  2. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  6. Be the first to take a peek at upcoming 2D Animation Preview. Drop into the forum and check it out!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    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


    Unity Technologies

    Dec 1, 2011
    Hello All!

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

    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".


    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):
    2. var actions = new Action[3];
    4. var j = 0;
    7. foreach (var c in "bar")
    9.     actions[j++] = () => Console.WriteLine(c);
    12. foreach (var a in actions)
    14.     a();
    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:

    Does this fix the 'foreach' allocates issue?

    Yes, under certain circumstances. See an explanation of the issue

    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.