Csharp Differences from JS

From Unify Community Wiki
Revision as of 10:46, 10 January 2012 by NCarter (Talk | contribs)

Jump to: navigation, search



Although the Unity Scripting page gives an overview of the differences in C# versus Javascript, it isn't comprehensive. I have run into several places where a tutorial in Javascript had some code that didn't translate directly into C#, and I had to do some research to find the C# version. So what other syntax differences are in C# versus JS?

Note: text copied from a Unity Answers question: Syntax Differences in C#/JS

Unity's Guide to C# Scripting

First, here is Unity's C# list: Writing Scripts in C#. It covers things like differences in coroutine syntax, etc.

Other Differences in C#

Function Definitions

Javascript functions are declared with the keyword function, whereas C# uses void. Void being the type for Unity functions - your own functions could have another return type, for instance it could be string. <javascript>function Start () { /* stuff. */ } // JS</javascript>

void Start() { /* stuff. */ } // C#
string MyFunc() { return "hello, world"; } // C#</csharp>
===== C# Generics =====
C# has Generic functions, which, ''very'' loosely speaking, are functions that have an attached meta-parameter to define what type of function it is. So for instance, Where the JS version is:
<javascript>function Start()
:var someScript : ExampleScript = GetComponent(ExampleScript);
The C# version has the type information attached directly to the function name:
<syntaxhighlight lang="csharp">void Start()
:var someScript = GetComponent<ExampleScript>();
:// Or a more verbose version of the line above:
:var someScript = (ExampleScript) GetComponent(typeof(ExampleScript));
So you attach your component name in brackets, to the function name. Here's an example from a project where I'm adding a ''LevelManager'' GameObject. The GO has an attached script, ''s_LevelManager'', that defines public functions, such as ''getMyCount''():
<syntaxhighlight lang="csharp">GameObject tmp = GameObject.Find("LevelManager");
if (tmp != null) {
:s_LevelManager slm = tmp.GetComponent<s_LevelManager>();
:int count = slm.getMyCount();
Note: unity iPhone 1.6 now supports generics (it didn't before). If you want a more complete definition of Generics:
[http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx MS Introduction to C# Generics]
===== The ''Foreach'' Keyword =====
C# Iterators use ''foreach'' instead of for:
<javascript>for(var x in someList) // JS
:x.someVar = true;</javascript>
<syntaxhighlight lang="csharp">foreach(var x in someList) // C#
:x.someVar = true;</csharp>
===== The ''New'' Keyword =====
In JS when creating a new object by calling its Class name, you just use the Class name, whereas C# requires the ''new'' keyword. Note - this includes creating new objects inside function calls:
<javascript>var foo = Vector3(0, 0, 0); // JS</javascript>
<syntaxhighlight lang="csharp">var foo = new Vector3(0, 0, 0); // C#
Instantiate(someObj, new Vector3(0, 0, 0), someRotation); // C#</csharp>
===== Casting =====
Speaking of ''Instantiate''() - it requires ''casting'' to use the returned object:
<javascript>GameObject foo = Instantiate(someObj...); // JS</javascript>
<syntaxhighlight lang="csharp">var foo = (GameObject) Instantiate(someObj...); // C#
var foo = Instantiate(someObj...) as GameObject; // C#</csharp>
'''Note''' that there are two different ways of casting in C#. For the first line above - if the object can't be instantiated, it will throw an Exception. You would need to use a ''try/catch'' to properly handle it. The second line, if it fails, will set ''foo'' to ''null'', and not throw an Exception. Then you would just need to test if the returned object was ''null'' or not. Also, because there's the possibility for the variable to be set to null, this casting method only works for reference types; while the other method works for value types as well as reference types.
===== Properties with Getters/Setters =====
In C#, it's possible to define special functions that can be accessed as if they were variables. For instance, I could say ''foo.someVar = "testing";'', and under the hood, there's a ''get'' and ''set'' function that process the argument "testing" and store it internally. But - they could also do any other processing on it, for instance, capitalizing the first letter before storing it. So you're not just doing a variable assignment, you're calling a function that sets the variable, and it can do - whatever functions do.
I'm not going into the syntax here, this answer is long enough :) But here are two links:
[http://msdn.microsoft.com/en-us/library/w86s7x04(VS.80).aspx MS: Using Properties] and 
[[http://www.csharp-station.com/Tutorials/Lesson10.aspx C# Properties Tutorial]]
== Concluding Thoughts ==
One of the discussions that prompted me to compile this list, was from someone who planned to learn Javascript instead of C#, because most of the Unity tutorials were in JS. There's some merit to that, especially if you're also new to programming and are doing a lot of cut/pasting from the tutorials :) But in the long run, it's '''not''' the best reason to pick a language. The fact is, C# and Javascript are 90% ''identical''. In most cases, only a few simple changes are needed to change a JS script to C#.
So, if they're nearly identical, why ''would'' you pick C#? That's a good way to start a religious war :) and I'm not going to try to justify one over the other here. I ''will'' say that the reason I went with C# is purely for the editor - Microsoft has a free version of Visual C# Express, which is well-integrated with Unity. It has Intellisense auto-complete, and tool-tip help that includes function parameters. Having a powerful editor really speeds up coding, and, so far anyway, I don't know of a similar (free) editor for Javascript.
Personal tools