-
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#|Boo|Remarks <\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> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
``` C# CODE ``` <\td> | ```boo BOO CODE ``` <\td> | REMARKS <\td> <\tr> |
Other Notes on Differences Between C# and Boo
These are some early notes from looking at the C# language specification. Assigning multiple variables at once
In C#, you can assign a value to two variables at once like so:
int a, b = 1
In boo, however, "a, b" refers to a sequence. So you would instead use:
a = b = 1
Note also declaring the type is unnecessary because of boo's Type Inference.
Use "a, b" when unpacking multiple values. For example:
name = "First Last" firstname, lastname = @/ /.Split(name) print firstname, lastname
Also note in boo you can use the "print" statement instead of System.Console.WriteLine. Buffer overflow checking
You can turn off overflow checking in boo like so:
try: checked: i = 100000 i += 1000000000 i += 1000000000 i += 1000000000 except: print "did overflow i"
unchecked: j = 100000 j += 1000000000 j += 1000000000 j += 1000000000 print "didn't overflow j: $j"
Variable number of parameters.
static void F(params int[] args) { Console.WriteLine("# of arguments: {0}", args.Length); for (int i = 0; i < args.Length; i++) Console.WriteLine("\targs[{0}] = {1}", i, args[i]); }
Boo uses syntax similar to python:
def F(*args as (int)): print "# of arguments: $(len(args))" for arg in args: print arg
By reference and output parameters.
In C# you can pass types to functions by reference using "ref" or "out" keywords.
static void Swap(ref int a, ref int b) { int t = a; a = b; b = t; } .... Swap(ref x, ref y);
Boo supports the "ref" keyword, too, but not "out", which is unnecessary in boo.
def Swap(ref a as int, ref b as int): t = a a = b b = t x = 1 y = 2 Swap(x,y)
Note though that in this particular sample, you can swap two values more easily like so:
x = 1 y = 2
x, y = y, x print x, y //-> 2 1
As a more general alternative to by reference parameters, in boo you can also return multiple values from a function instead:
def Swap(a as int, b as int): return b, a
a, b = Swap(a, b)
Things in C# but not Boo do..while loop
Boo doesn't have do..while or do..until loops like C# or VB.NET.
You can emulate them with "while true" loops. unsafe code
static void WriteLocations(byte[] arr) { unsafe { fixed (byte* pArray = arr) { byte* pElem = pArray; for (int i = 0; i < arr.Length; i++) { byte value = *pElem; Console.WriteLine("arr[{0}] at 0x{1:X} is {2}", i, (uint)pElem, value); pElem++; } } } }