-
Notifications
You must be signed in to change notification settings - Fork 149
Differences with Csharp
This page notes some differences between C# and boo. C# to Boo Converter
The easiest way to see how code in boo differs from C# is to use the C# to boo converter included in the Boo AddIn For SharpDevelop.
The converter is also accessible online via a webform here: [http://developer.sharpdevelop.net/codeconvert.net][]
While viewing a C# file in SharpDevelop, you can select from the menu: Tools -> Convert C# to boo. Or you can convert an entire C# project at once by right-clicking on the project icon and selecting: Convert -> C# to boo. Note, however, the converter is still new and in development.
The converter will convert C# to legal boo code, but it will not show you the simplest way to write that code. For example, instead of "System.Console.WriteLine(error) ", in boo you can simply say "print x". And many times the type will be declared "x as int" when it is unnecessary since the type can be inferred by the boo compiler: "x = 3" or "m = MyClass()". C# vs. Boo Syntax Examples
Here is a table noting some diffences between C# and boo, based on this table from the Nemerle programming language site. See also the C# language specification.
Expressions
C# <\th> | Boo <\th> | Remarks <\th> <\tr> | |||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
``` int x = 3; string y = "foo"; FooBarQux fbq = make_fbq (); ``` <\td> | ```boo x = 3 y = "foo" fbq = make_fbq() ``` <\td> | Types are inferred, no semicolons needed <\td> <\tr> | |||||||||||||||||||||||||||||||||||||||
``` expr_1 = expr_2 = expr_3; ``` <\td> | ```boo expr_1 = expr_2 = expr_3 ``` <\td><\tr> | ||||||||||||||||||||||||||||||||||||||||
``` Class c = new Class (params); ``` <\td> | ```boo c = Class(params) ``` <\td> | No "new" keyword required <\td> <\tr> | |||||||||||||||||||||||||||||||||||||||
``` Class[] c = new Class [size] ``` <\td> | ```boo c = array(Class, size) ``` <\td> | See Lists and Arrays <\td> <\tr> | |||||||||||||||||||||||||||||||||||||||
``` GenericClass c = new GenericClass(); ``` <\td> | ```boo c = GenericClass[of int]() ``` <\td> | See Generics <\td> <\tr> | |||||||||||||||||||||||||||||||||||||||
``` Type[] l = new Type[] { expr_1, expr_1, ..., expr_n } ``` <\td> | ```boo l = (expr_1, expr_1, ..., expr_n) ``` <\td> | Types are inferred when you use arrays <\td> <\tr> | |||||||||||||||||||||||||||||||||||||||
``` if (cond) return foo; do_something (); return bar; ``` <\td> | ```boo if cond: return foo //You can also say: return foo if cond do_something() return bar ``` <\td><\tr> | ||||||||||||||||||||||||||||||||||||||||
``` if (cond) answer = 42; ``` <\td> | ```boo if cond: answer = 42 //or answer = 42 if cond ``` <\td><\tr> | ||||||||||||||||||||||||||||||||||||||||
``` if (!cond) answer = 42; ``` <\td> | ```boo answer = 42 if not cond ``` <\td><\tr> | ||||||||||||||||||||||||||||||||||||||||
``` try ... catch (FooException e) { ... } catch (BarException e) { ... } ``` <\td> | ```boo try: ... except e as FooException: ... except e as BarException: ... ``` <\td><\tr> | ||||||||||||||||||||||||||||||||||||||||
``` try { foo (); bar (); } catch (Exception e) { baz (); } finally { qux (); } ``` <\td> | ```boo try: foo() bar() except e: baz() ensure: qux() ``` <\td><\tr> | ||||||||||||||||||||||||||||||||||||||||
``` throw new System.ArgumentException ("foo"); ``` <\td> | ```boo raise System.ArgumentException("foo") ``` <\td> | Use "raise" to generate an exception. <\td> <\tr> | |||||||||||||||||||||||||||||||||||||||
``` type t = ((type) expr) ``` <\td> | ```boo t = expr cast type //or //null if cast fails: t = expr as type ``` <\td> | See Casting Types <\td> <\tr> | |||||||||||||||||||||||||||||||||||||||
``` using System.Windows.Forms; Button button = control as Button; if (button != null) ... else ... ``` <\td> | ```boo import System.Windows.Forms button = control as Button if button != null: ... else: ... ``` <\td><\tr> | ||||||||||||||||||||||||||||||||||||||||
``` using System; using SWF = System.Windows.Forms; using System.Xml; ... Console.WriteLine ("foo"); SWF.Form x = new SWF.Form (); XmlDocument doc = new XmlDocument (); ``` <\td> |
```boo
import System
import System.Windows.Forms as SWF
import System.Xml
print "foo" x = SWF.Form() doc = XmlDocument()
x++; ++x;
<\td><\tr> |
||||||||||||||||||||||||||||||||||||||||
``` readonly int X = 2; const int Y = 3; ``` <\td> | ```boo final X = 2 static final Y = 3 ``` <\td> | Read-only and constant fields.<\td> <\tr>
<\table>
Type definitions
|