Csharp Differences from JS

From Unify Community Wiki
Revision as of 23:48, 12 March 2010 by Cyclops (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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. <csharp>function Start () { // stuff. } // JS 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); someScript.DoSomething (); }</javascript> The C# version has the type information attached directly to the function name: <csharp>void Start(){ ExampleScript someScript = GetComponent<ExampleScript>(); someScript.DoSomething (); }</csharp> 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(): <csharp>GameObject tmp = GameObject.Find("LevelManager"); if (tmp != null) {

   s_LevelManager slm = tmp.GetComponent<s_LevelManager>();
   int count = slm.getMyCount();

}</csharp> Note: unity iphone 1.6 now supports generics (it didn't before). If you want a more complete definition of Generics: MS Introduction to C# Generics

The Foreach Keyword

C# Iterators use foreach instead of for: <csharp>for (var x in someList) //JS

   x.someVar = true;

foreach (GameObject 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: <csharp>var foo = Vector3(0,0,0); // JS Vector3 foo = new Vector3(0,0,0); // C# Instantiate(someObj, new Vector3(0,0,0), someRotation); // C#</csharp>


Speaking of Instantiate() - it requires casting to use the returned object: <csharp>GameObject foo = Instantiate(someObj...); // JS GameObject foo = (GameObject) Instantiate(someObj...); // C# </csharp>

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: MS: Using Properties and [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