Comparison of Programming Languages

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Other Flow of Control Statements: Add and fixed C# syntax.)
m (added UnityScript notice and updated brief description)
 
(24 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category: Programming]]
+
{{UnityScript Notice}}
[[Category: Boo]]
+
[[Category: C Sharp]]
+
[[Category: JavaScript]]
+
 
+
 
+
This is a comparison of the features and instructions of the three high-level programming languages offered by Unity: [http://en.wikipedia.org/wiki/JavaScript JavaScript], [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#], and [http://en.wikipedia.org/wiki/Boo_(programming_language) Boo].
+
 
+
  
 +
This is a comparison of the features and instructions of the three high-level programming languages offered by Unity: UnityScript/JavaScript, [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#], and [http://en.wikipedia.org/wiki/Boo_(programming_language) Boo]. '''Update:''' As of version 2018.2, Unity now only officially supports the C# programming language.
  
 
== Conventions of This Article ==
 
== Conventions of This Article ==
Line 14: Line 8:
  
 
The following tables compare code differences of the Unity programming languages. See also the [[Comparison of Programming Languages]] for general information.
 
The following tables compare code differences of the Unity programming languages. See also the [[Comparison of Programming Languages]] for general information.
 
 
  
 
== Features ==
 
== Features ==
Line 24: Line 16:
 
|-  
 
|-  
 
! {{wikitable-th}} Language
 
! {{wikitable-th}} Language
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Boo_(programming_language) Boo]
 
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]
 
! {{wikitable-th}} UnityScript/[http://en.wikipedia.org/wiki/JavaScript JavaScript]
 
! {{wikitable-th}} UnityScript/[http://en.wikipedia.org/wiki/JavaScript JavaScript]
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Programming_paradigm Paradigm](s)
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Programming_paradigm Paradigm](s)
| {{wikitable-td}}
 
* [http://en.wikipedia.org/wiki/Imperative_programming imperative]
 
* [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented]
 
* [http://en.wikipedia.org/wiki/Functional_programming functional]
 
* [http://en.wikipedia.org/wiki/Generic_programming generic]
 
* [http://en.wikipedia.org/wiki/Reflective_programming reflective]
 
 
| {{wikitable-td}}  
 
| {{wikitable-td}}  
 
* [http://en.wikipedia.org/wiki/Imperative_programming imperative]
 
* [http://en.wikipedia.org/wiki/Imperative_programming imperative]
Line 48: Line 33:
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Standardization Standardized] Variation?
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Standardization Standardized] Variation?
| {{wikitable-yes}}
 
 
| {{wikitable-yes}}, ECMA, ISO ECMA-334; ISO/IEC 23270:2006
 
| {{wikitable-yes}}, ECMA, ISO ECMA-334; ISO/IEC 23270:2006
 
| {{wikitable-no}}
 
| {{wikitable-no}}
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Strongly-typed_programming_language Type Strength]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Strongly-typed_programming_language Type Strength]
| {{wikitable-td}} strong, duck
 
 
| {{wikitable-td}} strong
 
| {{wikitable-td}} strong
 
| {{wikitable-td}} strong<sup>1</sup>, duck
 
| {{wikitable-td}} strong<sup>1</sup>, duck
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Type_safety Type Safety]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Type_safety Type Safety]
| {{wikitable-td}} safe
 
 
| {{wikitable-td}} safe
 
| {{wikitable-td}} safe
 
| {{wikitable-td}} safe
 
| {{wikitable-td}} safe
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Type_system#Explicit_or_implicit_declaration_and_inference Expression of Types]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Type_system#Explicit_or_implicit_declaration_and_inference Expression of Types]
| {{wikitable-td}} implicit with optional explicit typing
 
 
| {{wikitable-td}} explicit
 
| {{wikitable-td}} explicit
 
| {{wikitable-td}} implicit with optional explicit typing
 
| {{wikitable-td}} implicit with optional explicit typing
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Type_system#Compatibility:_equivalence_and_subtyping Compatibility Among Composite Types]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Type_system#Compatibility:_equivalence_and_subtyping Compatibility Among Composite Types]
| {{wikitable-?}}
 
 
| {{wikitable-td}} name-based
 
| {{wikitable-td}} name-based
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Type_system#Type_checking Type Checking]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Type_system#Type_checking Type Checking]
| {{wikitable-td}} static with optional dynamic typing
 
 
| {{wikitable-td}} static with optional dynamic typing
 
| {{wikitable-td}} static with optional dynamic typing
 
| {{wikitable-td}} static with optional dynamic typing
 
| {{wikitable-td}} static with optional dynamic typing
Line 91: Line 70:
 
! {{wikitable-th}} UnityScript/[http://en.wikipedia.org/wiki/JavaScript JavaScript]
 
! {{wikitable-th}} UnityScript/[http://en.wikipedia.org/wiki/JavaScript JavaScript]
 
|-
 
|-
! {{wikitable-th}} 8 bit ([http://en.wikipedia.org/wiki/Byte byte]) Signed
+
! {{wikitable-th}} 8-bit ([http://en.wikipedia.org/wiki/Byte byte]) Signed
 
| {{wikitable-td}} sbyte
 
| {{wikitable-td}} sbyte
 
| {{wikitable-td}} sbyte
 
| {{wikitable-td}} sbyte
 
| {{wikitable-td}} sbyte
 
| {{wikitable-td}} sbyte
 
|-
 
|-
! {{wikitable-th}} 8 bit ([http://en.wikipedia.org/wiki/Byte byte]) Unsigned
+
! {{wikitable-th}} 8-bit ([http://en.wikipedia.org/wiki/Byte byte]) Unsigned
 
| {{wikitable-td}} byte
 
| {{wikitable-td}} byte
 
| {{wikitable-td}} byte
 
| {{wikitable-td}} byte
 
| {{wikitable-td}} byte
 
| {{wikitable-td}} byte
 
|-
 
|-
! {{wikitable-th}} 16 bit ([http://en.wikipedia.org/wiki/Short_integer short integer]) Signed
+
! {{wikitable-th}} 16-bit ([http://en.wikipedia.org/wiki/Short_integer short integer]) Signed
 
| {{wikitable-td}} short
 
| {{wikitable-td}} short
 
| {{wikitable-td}} short
 
| {{wikitable-td}} short
 
| {{wikitable-td}} short
 
| {{wikitable-td}} short
 
|-
 
|-
! {{wikitable-th}} 16 bit ([http://en.wikipedia.org/wiki/Short_integer short integer]) Unsigned
+
! {{wikitable-th}} 16-bit ([http://en.wikipedia.org/wiki/Short_integer short integer]) Unsigned
 
| {{wikitable-td}} ushort
 
| {{wikitable-td}} ushort
 
| {{wikitable-td}} ushort
 
| {{wikitable-td}} ushort
 
| {{wikitable-td}} ushort
 
| {{wikitable-td}} ushort
 
|-
 
|-
! {{wikitable-th}} 32 bit Signed
+
! {{wikitable-th}} 32-bit Signed
 
| {{wikitable-td}} int
 
| {{wikitable-td}} int
 
| {{wikitable-td}} int
 
| {{wikitable-td}} int
 
| {{wikitable-td}} int
 
| {{wikitable-td}} int
 
|-
 
|-
! {{wikitable-th}} 32 bit Unsigned
+
! {{wikitable-th}} 32-bit Unsigned
 
| {{wikitable-td}} uint
 
| {{wikitable-td}} uint
 
| {{wikitable-td}} uint
 
| {{wikitable-td}} uint
 
| {{wikitable-td}} uint
 
| {{wikitable-td}} uint
 
|-
 
|-
! {{wikitable-th}} 64 bit ([http://en.wikipedia.org/wiki/Long_integer long integer]) Signed
+
! {{wikitable-th}} 64-bit ([http://en.wikipedia.org/wiki/Long_integer long integer]) Signed
 
| {{wikitable-td}} long
 
| {{wikitable-td}} long
 
| {{wikitable-td}} long
 
| {{wikitable-td}} long
 
| {{wikitable-td}} long
 
| {{wikitable-td}} long
 
|-
 
|-
! {{wikitable-th}} 64 bit [http://en.wikipedia.org/wiki/Long_integer long integer]) Unsigned
+
! {{wikitable-th}} 64-bit [http://en.wikipedia.org/wiki/Long_integer long integer]) Unsigned
 
| {{wikitable-td}} ulong
 
| {{wikitable-td}} ulong
 
| {{wikitable-td}} ulong
 
| {{wikitable-td}} ulong
Line 201: Line 180:
 
|-
 
|-
 
! {{wikitable-th}} one-dimensional fixed size array
 
! {{wikitable-th}} one-dimensional fixed size array
| {{wikitable-td}} <boo>(type)</boo>
+
| {{wikitable-td}} <syntaxhighlight lang="boo">(type)</syntaxhighlight>
| {{wikitable-td}} <csharp>type[size]</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">type[size]</syntaxhighlight>
| {{wikitable-td}} <javascript>type[size]</javascript>
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">type[size]</syntaxhighlight>
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Array#Multi-dimensional_array multi-dimensional] fixed size array
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Array#Multi-dimensional_array multi-dimensional] fixed size array
| {{wikitable-?}}
+
| {{wikitable-td}} <syntaxhighlight lang="boo">array(type, size1, size2, ...)</syntaxhighlight>
| {{wikitable-td}} <csharp>type[size1, size2,...]</csharp>
+
or
 +
<syntaxhighlight lang="boo">(type, ndims)</syntaxhighlight>
 +
| {{wikitable-td}} <syntaxhighlight lang="csharp">type[size1, size2,...]</syntaxhighlight>
 
or
 
or
<csharp>type[size1][size2]...</csharp>
+
<syntaxhighlight lang="csharp">type[size1][size2]...</syntaxhighlight>
| {{wikitable-td}} <javascript>type[size1, size2,...]</javascript>
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">type[size1, size2,...]</syntaxhighlight>
 
(note: currently these types of arrays can be created implicitly and used, but cannot be created explicitly)
 
(note: currently these types of arrays can be created implicitly and used, but cannot be created explicitly)
 
|-
 
|-
 
! {{wikitable-th}} one-dimensional dynamic size array
 
! {{wikitable-th}} one-dimensional dynamic size array
| {{wikitable-td}} <boo>List</boo>
+
| {{wikitable-td}} <syntaxhighlight lang="boo">myList = [item1, item2, ...]</syntaxhighlight>
 
or
 
or
<boo>System.Collections.Generic.List[type]</boo>
+
<syntaxhighlight lang="boo">List</syntaxhighlight>
 
or
 
or
<boo>System.Collections.ArrayList</boo>
+
<syntaxhighlight lang="boo">System.Collections.Generic.List[type]</syntaxhighlight>
| {{wikitable-td}} <csharp>System.Collections.ArrayList</csharp>
+
 
or
 
or
<csharp>System.Collections.Generic.List<type></csharp>
+
<syntaxhighlight lang="boo">System.Collections.ArrayList</syntaxhighlight>
| {{wikitable-td}} <javascript>Array</javascript>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">System.Collections.ArrayList</syntaxhighlight>
 
or
 
or
<javascript>System.Collections.ArrayList</javascript>
+
<syntaxhighlight lang="csharp">System.Collections.Generic.List<type></syntaxhighlight>
 +
| {{wikitable-td}} <syntaxhighlight lang="javascript">Array</syntaxhighlight>
 +
or
 +
<syntaxhighlight lang="javascript">System.Collections.ArrayList</syntaxhighlight>
 +
or
 +
<syntaxhighlight lang="javascript">System.Collections.Generic.List.<type></syntaxhighlight>
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Array#Multi-dimensional_array multi-dimensional] dynamic size array
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Array#Multi-dimensional_array multi-dimensional] dynamic size array
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-td}} <csharp>System.Collections.Generic.List<System.Collections.Generic.List<...>></csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">System.Collections.Generic.List<System.Collections.Generic.List<...>></syntaxhighlight>
| {{wikitable-?}}
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">System.Collections.Generic.List.<System.Collections.Generic.List.<...>></syntaxhighlight>
 
|}
 
|}
  
Line 242: Line 227:
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Record_(computer_science) Record] Simple Composite Types
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Record_(computer_science) Record] Simple Composite Types
| {{wikitable-?}}
+
| {{wikitable-td}} <syntaxhighlight lang="boo">struct name:
| {{wikitable-td}} <csharp>struct name { type name; ... }</csharp>
+
⇥  name as type
| {{wikitable-td}} via objects: <javascript>class name { type name; ... }</javascript>
+
⇥  ...</syntaxhighlight>
 +
| {{wikitable-td}} <syntaxhighlight lang="csharp">struct name { type name; ... }</syntaxhighlight>
 +
| {{wikitable-td}} via objects: <syntaxhighlight lang="javascript">class name { type name; ... }</syntaxhighlight>
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Enumerated_type Enumeration] Simple Composite Types
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Enumerated_type Enumeration] Simple Composite Types
| {{wikitable-td}} <boo>enum condition:
+
| {{wikitable-td}} <syntaxhighlight lang="boo">enum condition:
⇥ item1
+
  item1
«item2
+
  item2
…»</boo>
+
  ...</syntaxhighlight>
| {{wikitable-td}} <csharp>enum name «: integral-type» { name «= value», ... }</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">enum name «: integral-type» { name «= value», ... }</syntaxhighlight>
| {{wikitable-td}} <javascript>enum type { name, ... }</javascript>
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">enum type { name, ... }</syntaxhighlight>
 
|}
 
|}
  
Line 265: Line 252:
 
|-
 
|-
 
! {{wikitable-th}} array
 
! {{wikitable-th}} array
 +
| {{wikitable-td}} <syntaxhighlight lang="boo">Boo.Lang.List</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-?}}
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">Array</syntaxhighlight>
| {{wikitable-td}} <javascript>Array</javascript>
+
 
|}
 
|}
 
  
 
== Declarations ==
 
== Declarations ==
Line 281: Line 267:
 
|-
 
|-
 
! {{wikitable-th}} variable
 
! {{wikitable-th}} variable
| {{wikitable-td}} <boo>name = initial_value</boo>
+
| {{wikitable-td}} <syntaxhighlight lang="boo">name = initial_value</syntaxhighlight>
| {{wikitable-td}} <csharp>type name «= initial_value»;</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">type name «= initial_value»;</syntaxhighlight>
| {{wikitable-td}} <javascript>var name «= initial_value»;</javascript>
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">var name «= initial_value»;</syntaxhighlight>
 
|-
 
|-
 
! {{wikitable-th}} constant
 
! {{wikitable-th}} constant
| {{wikitable-td}} <boo>final name = initial_value</boo>
+
| {{wikitable-td}} <syntaxhighlight lang="boo">final name = initial_value</syntaxhighlight>
| {{wikitable-td}} <csharp>const type name = value;</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">const type name = value;</syntaxhighlight>
| {{wikitable-td}} <javascript>N/A</javascript>
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">N/A</syntaxhighlight>
 
|-
 
|-
 
! {{wikitable-th}} type synonym
 
! {{wikitable-th}} type synonym
| {{wikitable-td}} <boo>synonym = typeof(type)</boo>
+
| {{wikitable-td}} <syntaxhighlight lang="boo">synonym = typeof(type)</syntaxhighlight>
| {{wikitable-td}} <csharp>using synonym = type;</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">using synonym = type;</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|}
 
|}
Line 308: Line 294:
 
|-
 
|-
 
! {{wikitable-th}} if
 
! {{wikitable-th}} if
| {{wikitable-td}} <boo>if (condition):
+
| {{wikitable-td}} <syntaxhighlight lang="boo">if (condition):
 
     instructions
 
     instructions
 
else:
 
else:
     instructions</boo>
+
     instructions</syntaxhighlight>
| {{wikitable-td}} <csharp>if (condition) { instructions; }
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">if (condition) { instructions; }
«else { instructions; }»</csharp>
+
«else { instructions; }»</syntaxhighlight>
| {{wikitable-td}} <javascript>if (condition) { instructions; }
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">if (condition) { instructions; }
«else { instructions; }»</javascript>
+
«else { instructions; }»</syntaxhighlight>
 
|-
 
|-
 
! {{wikitable-th}} else if
 
! {{wikitable-th}} else if
| {{wikitable-td}} <boo>if (condition):
+
| {{wikitable-td}} <syntaxhighlight lang="boo">if (condition):
 
instructions
 
instructions
 
elif:
 
elif:
instructions</boo>
+
instructions</syntaxhighlight>
| {{wikitable-td}} <csharp>if (condition) { instructions; }
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">if (condition) { instructions; }
 
else if (condition) { instructions; }
 
else if (condition) { instructions; }
 
...
 
...
«else { instructions; }»</csharp>
+
«else { instructions; }»</syntaxhighlight>
| {{wikitable-td}} <javascript>if (condition) { instructions; }
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">if (condition) { instructions; }
 
else if (condition) { instructions; }
 
else if (condition) { instructions; }
 
...
 
...
«else { instructions; }»</javascript>
+
«else { instructions; }»</syntaxhighlight>
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Switch_statement select case]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Switch_statement select case]
| {{wikitable-?}}  
+
| {{wikitable-td}} (none)
| {{wikitable-td}} <csharp>switch (variable) {
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">switch (variable) {
 
case case1: instructions; jump-statement;
 
case case1: instructions; jump-statement;
 
...
 
...
 
«default: instructions; jump-statement;»
 
«default: instructions; jump-statement;»
}</csharp>
+
}</syntaxhighlight>
| {{wikitable-td}} <javascript>switch (variable) {
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">switch (variable) {
 
case case1: instructions; jump-statement;
 
case case1: instructions; jump-statement;
 
...
 
...
 
«default: instructions; jump-statement;»
 
«default: instructions; jump-statement;»
}</javascript>
+
}</syntaxhighlight>
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Conditional_(programming)#If_expressions conditional expression (ternary)]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Conditional_(programming)#If_expressions conditional expression (ternary)]
| {{wikitable-n/a}}
+
| {{wikitable-td}} <syntaxhighlight lang="boo">valueIfTrue if condition else valueIfFalse</syntaxhighlight>
| {{wikitable-td}} <csharp>condition ? valueIfTrue : valueIfFalse;</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">condition ? valueIfTrue : valueIfFalse;</syntaxhighlight>
| {{wikitable-td}} <javascript>condition ? valueIfTrue : valueIfFalse;</javascript>
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">condition ? valueIfTrue : valueIfFalse;</syntaxhighlight>
 
|}
 
|}
  
Line 360: Line 346:
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/While_loop while]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/While_loop while]
| {{wikitable-td}} <boo>while (condition):
+
| {{wikitable-td}} <syntaxhighlight lang="boo">while (condition):
     instructions</boo>
+
     instructions</syntaxhighlight>
| {{wikitable-td}} <csharp>while (condition) { instructions; }</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">while (condition) { instructions; }</syntaxhighlight>
| {{wikitable-td}} <javascript>while (condition) { instructions; }</javascript>
+
| {{wikitable-td}} <syntaxhighlight lang="javascript">while (condition) { instructions; }</syntaxhighlight>
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Do_while_loop do-while]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Do_while_loop do-while]
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-td}} <csharp>do { instructions; } while(condition);</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">do { instructions; } while(condition);</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/For_loop for]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/For_loop for]
| {{wikitable-td}} <boo>for i in range(first, last):
+
| {{wikitable-td}} <syntaxhighlight lang="boo">for i in range(first, last):
     instructions</boo>
+
     instructions</syntaxhighlight>
| {{wikitable-td}} <csharp>for («initializer»; «condition»; «modifier») { instructions; }</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">for («initializer»; «condition»; «modifier») { instructions; }</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Foreach foreach]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Foreach foreach]
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-td}} <csharp>foreach (type name in collection) { instructions; }</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">foreach (type name in collection) { instructions; }</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|}
 
|}
Line 393: Line 379:
 
! {{wikitable-th}} throw
 
! {{wikitable-th}} throw
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-td}} <csharp>throw new exception-type(«arguments»);</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">throw new exceptionType(«arguments»);</syntaxhighlight>
 
or
 
or
<csharp>throw exception-variable;</csharp>
+
<syntaxhighlight lang="csharp">throw exceptionVariable;</syntaxhighlight>
 
or
 
or
<csharp>throw; // re-throws an exception; can only be used inside a catch block</csharp>
+
<syntaxhighlight lang="csharp">throw; // re-throws an exception; can only be used inside a catch block</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} handler
 
! {{wikitable-th}} handler
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-td}} <csharp>try { instructions; }
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">try { instructions; }
«catch(exception-type «variable») { «instructions;» }»
+
«catch(exceptionType «variable») { «instructions;» }»
 
...
 
...
catch«(exception-type «variable»)» { «instructions;» }</csharp>
+
catch«(exceptionType «variable»)» { «instructions;» }</syntaxhighlight>
 
or
 
or
<csharp>try { instructions; }
+
<syntaxhighlight lang="csharp">try { instructions; }
finally { instructions; }</csharp>
+
finally { instructions; }</syntaxhighlight>
 
or
 
or
<csharp>try { instructions; }
+
<syntaxhighlight lang="csharp">try { instructions; }
«catch(exception-type «variable») { «instructions;» }»
+
«catch(exceptionType «variable») { «instructions;» }»
 
...
 
...
catch«(exception-type «variable»)» { «instructions;» }
+
catch«(exceptionType «variable»)» { «instructions;» }
finally { instructions; }</csharp>
+
finally { instructions; }</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} assertion
 
! {{wikitable-th}} assertion
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-td}} <csharp>System.Diagnostics.Debug.Assert(condition «, message «, detail-message»»);</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">System.Diagnostics.Debug.Assert(condition «, message «, detailsMessage»»);</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|}
 
|}
Line 433: Line 419:
 
|-
 
|-
 
! {{wikitable-th}} exit block(break)
 
! {{wikitable-th}} exit block(break)
| {{wikitable-td}} <boo>break</boo>
+
| {{wikitable-td}} <syntaxhighlight lang="boo">break</syntaxhighlight>
| {{wikitable-td}} <csharp>break;</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">break;</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} continue
 
! {{wikitable-th}} continue
| {{wikitable-td}} <boo>continue</boo>
+
| {{wikitable-td}} <syntaxhighlight lang="boo">continue</syntaxhighlight>
| {{wikitable-td}} <csharp>continue;</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">continue;</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Label_(programming_language) label]
 
! {{wikitable-th}} [http://en.wikipedia.org/wiki/Label_(programming_language) label]
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-td}} <csharp>label:</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">labelName:</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} branch ([http://en.wikipedia.org/wiki/Goto goto])
 
! {{wikitable-th}} branch ([http://en.wikipedia.org/wiki/Goto goto])
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-td}} <csharp>goto label;</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">goto labelName;</syntaxhighlight>
 
or
 
or
<csharp>goto case case-label; // can only be used in a switch block</csharp>
+
<syntaxhighlight lang="csharp">goto case caseValue; // can only be used in a switch block</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
Line 471: Line 457:
 
! {{wikitable-th}} calling a function
 
! {{wikitable-th}} calling a function
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-th}} <csharp>functionName(args1, args2,...)</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">functionName(arg1, arg2, ...);</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} basic/void function
 
! {{wikitable-th}} basic/void function
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-th}} <csharp>void functionName(type param1, type param2){ instructions }</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">void functionName(type param1, type param2, ...) { instructions; }</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} value-returning function
 
! {{wikitable-th}} value-returning function
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-th}} <csharp>Type functionName (type param1, type param2){ instructions; return valueOfFunctionType; }</csharp>
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">type functionName(type param1, type param2, ...) { instructions; return valueOfFunctionType; }</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|}
 
|}
Line 496: Line 482:
 
! {{wikitable-th}} basic type to basic type
 
! {{wikitable-th}} basic type to basic type
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-?}}
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">(type) name</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} string to basic type
 
! {{wikitable-th}} string to basic type
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-?}}
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">type.Parse(name) /* THIS IS HIGHLY DEPENDENT UPON THE BASIC TYPE */</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} basic type to string
 
! {{wikitable-th}} basic type to string
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-?}}
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">name.ToString()</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} complex type to string
 
! {{wikitable-th}} complex type to string
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-?}}
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">name.ToString()</syntaxhighlight>
 
| {{wikitable-?}}
 
| {{wikitable-?}}
 
|-
 
|-
 
! {{wikitable-th}} complex type to complex type
 
! {{wikitable-th}} complex type to complex type
 
| {{wikitable-?}}
 
| {{wikitable-?}}
| {{wikitable-?}}
+
| {{wikitable-td}} <syntaxhighlight lang="csharp">(type) name</syntaxhighlight>
| {{wikitable-td}} <javascript>name as type</javascript>
+
or
 +
<syntaxhighlight lang="csharp">name as type</syntaxhighlight>
 +
| {{wikitable-td}} <syntaxhighlight lang="javascript">name as type</syntaxhighlight>
 
|}
 
|}
  
Line 524: Line 512:
  
 
[[Programming|Programming Index]]
 
[[Programming|Programming Index]]
 +
 +
[[Category: Legacy]]
 +
[[Category: Boo]]
 +
[[Category: JavaScript]]

Latest revision as of 01:39, 22 November 2018

End of support for UnityScript

Starting with Unity 2018.2, support for UnityScript (Unity's version of JavaScript) has ended. C# is now the only supported language going forward. You can learn more about the reasons for this change here. There is an official UnityScript-to-C# Automatic Conversion tool to help those wanting to transition their UnityScript projects over to C#.


This is a comparison of the features and instructions of the three high-level programming languages offered by Unity: UnityScript/JavaScript, C#, and Boo. Update: As of version 2018.2, Unity now only officially supports the C# programming language.

Contents

[edit] Conventions of This Article

The bold is the literal code. The non-bold is interpreted by the reader. Statements in guillemets (« … ») are optional. “⇥ ” indicates a necessary indent.

The following tables compare code differences of the Unity programming languages. See also the Comparison of Programming Languages for general information.

[edit] Features

The following tables compares major features of the Unity programming languages.

Language C# UnityScript/JavaScript
Paradigm(s)
Standardized Variation? Yes, ECMA, ISO ECMA-334; ISO/IEC 23270:2006 No
Type Strength strong strong1, duck
Type Safety safe safe
Expression of Types explicit implicit with optional explicit typing
Compatibility Among Composite Types name-based ?
Type Checking static with optional dynamic typing static with optional dynamic typing

1 Normally Javascript in Unity uses type inference to statically type all variables when possible, in cases where the type is not explicitly stated. Dynamic typing can be forced if desired, and, in certain circumstances, dynamic typing is used unless forced otherwise. Use of the "#pragma strict" directive disables all dynamic typing.

[edit] Type Identifiers

[edit] Integer

Language Boo C# UnityScript/JavaScript
8-bit (byte) Signed sbyte sbyte sbyte
8-bit (byte) Unsigned byte byte byte
16-bit (short integer) Signed short short short
16-bit (short integer) Unsigned ushort ushort ushort
32-bit Signed int int int
32-bit Unsigned uint uint uint
64-bit (long integer) Signed long long long
64-bit long integer) Unsigned ulong ulong ulong

[edit] Floating Point

Language Boo C# UnityScript/JavaScript
Single Precision single float float
Double Precision double double double
Arbitrarily Precise (bignum) decimal decimal ?

[edit] Other Types

Language Boo C# UnityScript/JavaScript
Text Character N/A char char
Text String string string String
Boolean) bool bool boolean
Object/Universal) object object Object


[edit] Derived Types

[edit] Array

Language Boo C# UnityScript/JavaScript
one-dimensional fixed size array
(type)
type[size]
type[size]
multi-dimensional fixed size array
array(type, size1, size2, ...)

or

(type, ndims)
type[size1, size2,...]

or

type[size1][size2]...
type[size1, size2,...]

(note: currently these types of arrays can be created implicitly and used, but cannot be created explicitly)

one-dimensional dynamic size array
myList = [item1, item2, ...]

or

List

or

System.Collections.Generic.List[type]

or

System.Collections.ArrayList
System.Collections.ArrayList

or

System.Collections.Generic.List<type>
Array

or

System.Collections.ArrayList

or

System.Collections.Generic.List.<type>
multi-dimensional dynamic size array ?
System.Collections.Generic.List<System.Collections.Generic.List<...>>
System.Collections.Generic.List.<System.Collections.Generic.List.<...>>

[edit] Other Types

Language Boo C# UnityScript/JavaScript
Record Simple Composite Types
struct name:
⇥   name as type 
⇥   ...
struct name { type name; ... }
via objects:
class name { type name; ... }
Enumeration Simple Composite Types
enum condition:
⇥   item1
⇥   item2
⇥   ...
enum name «: integral-type» { name «= value», ... }
enum type { name, ... }

[edit] Basic Unity-Specific Types

Language Boo C# UnityScript/JavaScript
array
Boo.Lang.List
?
Array

[edit] Declarations

Language Boo C# UnityScript/JavaScript
variable
name = initial_value
type name «= initial_value»;
var name «= initial_value»;
constant
final name = initial_value
const type name = value;
N/A
type synonym
synonym = typeof(type)
using synonym = type;
?

[edit] Flow of Control

[edit] Conditional Statements

Language Boo C# UnityScript/JavaScript
if
if (condition):
    instructions
else:
    instructions
if (condition) { instructions; }
«else { instructions; }»
if (condition) { instructions; }
«else { instructions; }»
else if
if (condition):
	instructions
elif:
	instructions
if (condition) { instructions; }
else if (condition) { instructions; }
...
«else { instructions; }»
if (condition) { instructions; }
else if (condition) { instructions; }
...
«else { instructions; }»
select case (none)
switch (variable) {
	case case1: instructions; jump-statement;
	...
	«default: instructions; jump-statement;»
}
switch (variable) {
	case case1: instructions; jump-statement;
	...
	«default: instructions; jump-statement;»
}
conditional expression (ternary)
valueIfTrue if condition else valueIfFalse
condition ? valueIfTrue : valueIfFalse;
condition ? valueIfTrue : valueIfFalse;

[edit] Loop Statements

Language Boo C# UnityScript/JavaScript
while
while (condition):
    instructions
while (condition) { instructions; }
while (condition) { instructions; }
do-while ?
do { instructions; } while(condition);
?
for
for i in range(first, last):
    instructions
for («initializer»; «condition»; «modifier») { instructions; }
?
foreach ?
foreach (type name in collection) { instructions; }
?

[edit] Exceptions

Language Boo C# UnityScript/JavaScript
throw ?
throw new exceptionType(«arguments»);

or

throw exceptionVariable;

or

throw; // re-throws an exception; can only be used inside a catch block
?
handler ?
try { instructions; }
«catch(exceptionType «variable») { «instructions;» }»
...
catch«(exceptionType «variable»)» { «instructions;» }

or

try { instructions; }
finally { instructions; }

or

try { instructions; }
«catch(exceptionType «variable») { «instructions;» }»
...
catch«(exceptionType «variable»)» { «instructions;» }
finally { instructions; }
?
assertion ?
System.Diagnostics.Debug.Assert(condition «, message «, detailsMessage»»);
?

[edit] Other Flow of Control Statements

Language Boo C# UnityScript/JavaScript
exit block(break)
break
break;
?
continue
continue
continue;
?
label ?
labelName:
?
branch (goto) ?
goto labelName;

or

goto case caseValue; // can only be used in a switch block
?
return value from generator ? ? ?

[edit] Functions

Language Boo C# UnityScript/JavaScript
calling a function ?
functionName(arg1, arg2, ...);
?
basic/void function ?
void functionName(type param1, type param2, ...) { instructions; }
?
value-returning function ?
type functionName(type param1, type param2, ...) { instructions; return valueOfFunctionType; }
?

[edit] Type Conversions

Language Boo C# UnityScript/JavaScript
basic type to basic type ?
(type) name
?
string to basic type ?
type.Parse(name) /* THIS IS HIGHLY DEPENDENT UPON THE BASIC TYPE */
?
basic type to string ?
name.ToString()
?
complex type to string ?
name.ToString()
?
complex type to complex type ?
(type) name

or

name as type
name as type



Programming Index

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox