Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Does Unity use a different version of C#, or is it all the same? It looks different from regular C# but there are some regular C# elements in there.

share|improve this question

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

4  
What differences do you see? –  Anko Jul 15 at 9:30
    
I suspect the changes you are referring to are framework differences and not the C# language. In Unity3d you are using the .Net framework (Via Mono) and Unity specific framework elements with the C# language. –  Mike B Jul 15 at 15:51
    
It's just a very old version. If you want the latest version of C# and Mono and want to program rather than script a pre-made engine, then consider something like monogame.net –  Den Jul 16 at 12:17
    
I noticed slight library differences, for example in Unity there's a Mathf class for math functions, instead of Math class. I haven't paid attention if there are other differences, but that's the one that stood out. –  Alex Jul 18 at 18:29

5 Answers 5

up vote 17 down vote accepted

Unity 4 uses Mono 2.6, which is a full implementation of the .NET framework, including the C# language.

I'm not sure how it looks different, but keep in mind that Unity supports several languages, all of which work on top of the same Mono runtime. Is it possible you're confusing C# with UnityScript?

share|improve this answer
6  
To add, Mono 2.6 is equivalent to C# 3.0 in terms of language features. Mono 2.6.1 started adding C# 4.0 features. –  Neeko Jul 15 at 13:46
    
Here is a nice overview of Unity's Mono Compatibility as of v4.1.2 –  Kelly Thomas Jul 16 at 5:26
    
It lags not just minor but a major version increment: mono-project.com/Release_Notes_Mono_3.4 –  Den Jul 16 at 12:19

Unity uses regular C#.

Then again, when you write C# in Unity you will be using a lot of their libraries, but as far as I know, everything possible in C# is possible in Unity, other than the differences listed below:

  • More specific areas of .Net relating to Windows Forms & ASP are off limits through Unity.

  • While you can use Visual Studio for editing and compile-time errors, you have to build and run in the Unity IDE.

  • Unity uses Mono which is an open source implementation of .Net which means there are slight differences.

share|improve this answer
    
To expand on your second bullet -- you can also debug within Visual Studio by using UnityVS. Microsoft recently acquired the developers so they'll be releasing the plugin for free in the near future. –  mrohlf Jul 15 at 16:17

As stated in other answers Unity 4.x uses a modified version of Mono based on Mono 2.6

For the most part, this is compatible with the .Net 2.0, though I haven't managed to track down a Mono 2.6 specific compatibility list.

It looks different from regular C# but there are some regular C# elements in there.

As mentioned in one of the comments on your question, this is likely due to Unity's particular scripting API rather than the language itself.

As an example, a lot of code in a typical Unity projects is contained in subclasses of a class called MonoBehavior. These are components that are dropped on GameObjects within the Unity Editor environment. This architecture leads to C# code that looks different to typical C# code (to me anyway) in a number of ways:

  • Until Unity 4 was released, these objects could not be contained in namespaces, so they're always in the global namespace
  • They expose fields to the editor environment by making them public (or by using the SerializeField attribute, but I find very few people use this), which leads to an unusually large number of public fields on classes
  • Unity's privacy and case conventions don't follow Microsoft's, so this can also look strange to a "traditional" C# developer
  • They make use of a number of special methods on these components, such as Start and Update, that are not overrides as one would typically expect, but are accessed by means of reflection instead.

Practically speaking, the biggest C# language feature I miss in Unity's current C# version is support for async and related keywords and functionality. A similar concept in Unity is coroutines. These execute on the main thread so aren't true async, but do allow long running code to be broken up over multiple frames. Lower level multithreading is still supported.

share|improve this answer

Some elements of the UnityEngine package conflict with standard C# naming, so it makes it appear to be different. However, as others have said, it is regular C#.

share|improve this answer

The question whether the C# Version used by Unity is different from a "regular" C# has been answered by other posts. Since you are explicitly asking for certain elements which might differ from such a regular version I will share the only minor difference I noticed when comparing the C# I use at work (i.e. C# 4.0 in Visual Studio 2010 and higher) and C# in Unity, which is that I can't use default values for parameters in function definitions. which is that you have to set up your project more carefully in order to use a newer version of C#. See this link for further explanation.

Edit: I leave this answer since as far as I know this is a misunderstanding that happens to quite a lot of people.

share|improve this answer
    
As that feature was only added to C# in version 4.0 and wasn't present before, its absence cannot really be called a "difference" to "C#". –  O. R. Mapper Jul 15 at 19:46
1  
C# Version 4 was released in 2010. I think a lot of people are using it and feel, like me, its absence when programming in Unity. –  Nessuno Jul 15 at 20:10
    
Are you sure you can't use default values? I'm pretty sure I do and have... –  NPSF3000 Jul 15 at 23:39
    
Well I tried and it won't compile with a message telling me that I can't use default values. Are you using a special version of Unity? Like the pro version? –  Nessuno Jul 16 at 13:47
1  
You can absolutely use default values, and it's nothing to do with pro or not-pro. Unity and MonoDevelop have an odd relationship. The auto-generated projects in MonoDevelop are often set to earlier versions of C# that don't support default values. However if you change that setting in the project, it will work just fine. If you ignore compilation in MonoDevelop, and simply go back to Unity, you'll note that Unity does not complain about the default parameters. –  MrCranky Jul 16 at 13:57

protected by Josh Petrie Jul 15 at 20:29

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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