- Home /
The type or namespace 'var' could not be found (in C#)
Does unity support the C# keyword var and if not are there plans to support it?
EDIT
I did some research and found that mono seems to support three versions of C#: 1, 2 and 3. In 2007, they added support to C# 3 for the var keyword. I'm not sure what version of mono is being used in Unity and if it should support var (maybe I'm doing something wrong).
Thanks!
Solution
As @The Stone and @Ricardo were nice enough to point out, Unity is currently supporting an older version of mono that doesn't yet support the C# 3 feature set (var, LINQ, etc). They are investigating it and would have implemented it with Unity 2.6, but it would break backwards compatibility.
Currently there is a lot of interesting discussion in the user voice page for it so feel free to read about it/vote it up if it is important to you.
Update
Hey all, with the Unity 3.0 announcement and a pending release in Summer 2010 it looks like this feature will be implemented. I can't wait.
the unity iphone 1.6 is upgraded to mono 2.6 so they will upgrade the unity too. they might do this in next $$anonymous$$or release.
Answer by TowerOfBricks · Nov 25, 2009 at 07:39 AM
Unity does not use the latest version of mono. The latest version is 2.4 and 2.6 will be out soon, right now Unity uses an 1x version, they haven't upgraded due to doing so will break all existing web player games. But I guess they will do so when Unity 3.0 comes, I hope so at least.
Thanks for the clarification. Its not the end of the world, but I'm with you. I really hope they update with version 3.0. I think I'll head over to user voice...
Aras mentioned on the feedback page they wanted to do it for 2.6, but had to delay it. There's no estimated deadline for it yet: http://feedback.unity3d.com/pages/15792-unity/suggestions/158233-scripting-upgrade-mono-to-2-4-2-6?ref=title
Answer by Lance Sun · Dec 09, 2009 at 01:52 AM
Here's a trick I am using to get C# 3.0 language support for var, extension methods, lambda expressions, object initializers, collection initializers, implicitly typed arrays, expression trees, auto implemented properties, and query expressions.
I've only done this for Windows. The Mac instructions are probably analogous.
Download and install the latest version of Mono (2.6.1)
Copy these files: gmcs.exe, gmcs.exe.config
From: C:\Program Files\Mono-2.6.1\lib\mono\2.0
To: \Unity\Editor\Data\MonoCompiler.framework
This only works because C# 3.0 compiles into IL compatible with the 2.0 runtime. Though, linq to objects does require a change to System.Core (which came with .NET Framework 2.0 SP1)... but linq code compiles and runs fine for me, so I guess the version of mono that Unity is using includes this change. What definitely doesn't work is linq to sql, because that requires System.Data.Linq
Holy moly! Thats brilliant, I'm sure it's not "officially" supported, but I'm going to try that out this weekend. If this works, you will have saved my butt. I'll come back and update once I've tested it. Thank you!
If I could downvote myself to vote you up more than once, I would. I can confirm this as working on OSX, even if the files you'll need to copy are slightly differently named. There's no mono.dll, and the libglib and libgmodule will have .dylib extensions. This will get you var declarations, collection initializers... the works.
To get this to work with $$anonymous$$ono 2.6 you must copy the file libgthread-2.0-0.dll from the mono bin folder, too. Thanks for this "hack"!!
whoops, you're right. and you don't need libgmodule-2.0.0.dll
Update: If you are using LINQ in your project, it will work. If you are using a prebuilt assembly targeting .NET 3.5, and that assembly uses LINQ, you will get an error message in Unity about System.Func`2`. You can copy C:\Program Files\$$anonymous$$ono-2.6.1\lib\mono\2.0\System.Core.dll to Unity\Editor\Data\$$anonymous$$onoCompiler.framework and Unity\Editor\Data\Frameworks\$$anonymous$$ono.framework to get it to work. But keep in $$anonymous$$d that since you are modifying the runtime framework, it won't work with the WebPlayer.
Answer by LoganBarnett · Apr 10, 2010 at 01:18 PM
I posted an upgrade script here: http://github.com/LoganBarnett/unity-mono-upgrade It's for OS X only. Feel free to submit a Windows version and I'll post it there.
Answer by Ricardo · Nov 25, 2009 at 02:29 PM
Your guess is correct TheLameDuck - Unity is still only up to C# 2.0. That means no var declaration, no nice collection initializers, and no Linq.
Answer by Jaap Kreijkamp · Nov 25, 2009 at 06:55 AM
No, in C# you define the variable by type so:
int x;
instead of:
var x;
and
GameObject go;
instead of:
var go : GameObject;
Thanks, but beginning with C# .net 3.0 $$anonymous$$S introduced the var keyword: http://msdn.microsoft.com/en-us/library/bb383973.aspx. It isn't like the JS equivalent in that it can't be set to multiple types, it is just a shorthand.
$$anonymous$$y guess is that the version of mono that is supported doesn't yet have the var keyword, but I was hoping to get confirmation. Its not a huge deal, but within the C# community its starting to become "good practice" to use var when initializing.
C# compiler will choose the type from righthand side data when see the var keyword in lefside of identifier name. i hope that and think that unity 3 will support latest version of mono's C# compiler that is C# 4 in that time. hopefully we have dynamic language features in C# for web services and wow optional parameters :D :D :D