天天看點

Boo Vs C# 語言對比 6

Boo syntax

C# equivalent

class Car:

pass

pass keyword

public class Car

{

}

employee is null

employee == null

"foo" is "bar"

employee isa Manager

ReferenceEquals("foo","bar")

employee is Manager

employee is not null

employee != null

not employee.IsTemporary

!employee.IsTemporary

employee is not null and

employee != null &&

employee is Manage

employee isa Manager or not

employee.IsTemporary

(employee is Manager) ||

(!employee.IsTempor

cast(int, variable)

(int)variable

name = variable as string

string name = variable as string;

typeOfString = typeof(string)

Type typeOfString = typeof(string);

typeOfString = string

if lives == 0:

print "game over"

if (lives == 0)

Console.WriteLine("game over");

game.Finish()

if( lives == 0)

game.Finish();

if not lives:

unless lives:

print "game over" unless lives

print "game over" if lives == 0

else:

print "carry on"

else

Console.Writeline("carry on");

elif lives == 1:

print "Last life"

else if (lives == 1)

Console.WriteLine("last life")

while lives != 0:

PlayRound()

while(lives!=0)

PlayRound();

for i in range(0,10):

print i

for(int i=0;i<10;i++)

Console.WriteLine(i);

for user in users:

print user.Name

if user.Name is null:

continue

foreach(User user in users)

Console.WriteLine(user.Name);

if(user.Name == null)

continue;

index = 0

break if index > 10

index += 1

int index = 0;

if(index > 10)

break;

index += 1;

while ie.Busy:

Thread.Sleep(50ms)

while( ie.Busy)

Thread.Sleep(50);

wheels as int

defStartEngine():

protected int wheels;

public void StartEngine()

internal class Car:

internal class Car {}

struct Point:

X as int

Y as int

public struct Point

public int X;

public int Y;

class Truck(Car):

public class Truck : Car

class Car(IDisposable):

def Dispose():

public class Car : IDisposable

public void Dispose()

enumTddStages:

Red

Green

Refactor

public enumTddStages

Red,

Green,

def Start():

public void Start()

def Start(async as bool):

public void Start(boolasync)

def Start(async as bool) as

WaitHandle:

raise NotImplementedException()

public WaitHandle Start(boolasync)

throw new

NotImplementedException();

defGetInteger():

return 1

public intGetInteger()

return 1;

defSetItem(key,val):

public void SetItem(object key, object val)

defVarArgs(*args as (object)):

VarArgs(1, "foo")

public void VarArgs(params object[]

args)

Name:

get:

return "Boo"

public string Name

get { return "C#"; }

Email:

return email

set:

email = value

get: return email

set: email = value

public string Email

get { return email; }

set { email = value; }

[property(Email)]

email as string

public string Email { get; set; }

xml = XmlDocument()

XmlDocument xml = new XmlDocument()

emp = Employee(Name: "foo", Id: 15)

varemp = new Employee{ Name = "foo",

Id = 15};

class Car():

def constructor():

print "Car created!"

public Car()

Console.WriteLine("Car

created!");

def destructor():

print "died"

public ~Car()

Console.WriteLine("died");

throw new NotImplementedException();

raise "error happened"

throw new Exception("error happened");

try:

# do something

except:

print "error happened"

try

// do something

catch

Console.WriteLine("error

happened");

except e as SoapException:

print e.Detail

except e:

print e

catch(SoapException e)

Console.WriteLine(e);

catch(Exception e)

ensure:

print "done"

finally

Console.WriteLine("done");

raise

throw;

save.Click += do(sender,e):

print "Clicked"

save.Click += delegate(

object sender,

EventArgs e

)

Console.WriteLine("Clicked");

save.Click += { print "Clicked" }

save.Click += (sender, e) =>

myList.Find( { i | i > 5 })

myList.Find(delegate(int i)

return i> 5;

});

namespace Foo.Bar.Baz

class Foobar:

public class Foobar

self.name = "foo";

this.name = "foo";

super.name = "foo";

base.name = "foo";

# single line comment

// and this is one as well

/*

And here is a multi

line comment

*/

// single line comment

Multi line comment

assert user is not null

Debug.Assert( user != null, "user is

not null"

print "foo"

Console.WriteLine("foo")

using db = RhinoConnection():

using(RhinoConnectiondb = new

RhinoConnection())

lock syncLock:

#code under lock

lock(syncLock)

// code under lock