Boo Vs C# 语言对比 6

 keywords

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 isa Manager

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

Type typeOfString = typeof(string);

Conditionals

if lives == 0:

print "game over"

if (lives == 0)

Console.WriteLine("game over");

if lives == 0:

print "game over"

game.Finish()

if( lives == 0)

{

Console.WriteLine("game over");

game.Finish();

}

if not lives:

print "game over"

if (lives == 0)

Console.WriteLine("game over");

unless lives:

print "game over"

if (lives == 0)

Console.WriteLine("game over");

print "game over" unless lives

print "game over" if lives == 0

if (lives == 0)

Console.WriteLine("game over");

if lives == 0:

print "game over"

else:

print "carry on"

if (lives == 0)

Console.WriteLine("game over");

else

Console.Writeline("carry on");

if lives == 0:

print "game over"

elif lives == 1:

print "Last life"

else:

print "carry on"

if (lives == 0)

Console.WriteLine("game over");

else if (lives == 1)

Console.WriteLine("last life")

else

Console.Writeline("carry on");

Loops and iterations

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

for user in users:

if user.Name is null:

continue

print user.Name

foreach(User user in users)

Console.WriteLine(user.Name);

foreach(User user in users)

{

if(user.Name == null)

continue;

Console.WriteLine(user.Name);

}

index = 0

for user in users:

break if index > 10

print user.Name

index += 1

int index = 0;

foreach(User user in users)

{

if(index > 10)

break;

Console.WriteLine(user.Name);

index += 1;

}

while ie.Busy:

Thread.Sleep(50ms)

while( ie.Busy)

Thread.Sleep(50);

Type declarations

class Car:

wheels as int

defStartEngine():

pass

public class Car

{

protected int wheels;

public void StartEngine()

{

}

}

internal class Car:

pass

internal class Car {}

struct Point:

X as int

Y as int

public struct Point

{

public int X;

public int Y;

}

class Truck(Car):

pass

public class Truck : Car

{

}

class Car(IDisposable):

def Dispose():

pass

public class Car : IDisposable

{

public void Dispose()

{

}

}

enumTddStages:

Red

Green

Refactor

public enumTddStages

{

Red,

Green,

Refactor

}

Methods, properties, and control structures

def Start():

pass

public void Start()

{

}

def Start(async as bool):

pass

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):

pass

public void SetItem(object key, object val)

{

}

defVarArgs(*args as (object)):

pass

VarArgs(1, "foo")

public void VarArgs(params object[]

args)

{

}

Name:

get:

return "Boo"

public string Name

{

get { return "C#"; }

}

Email:

get:

return email

set:

email = value

Email:

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 class Car

{

public Car()

{

Console.WriteLine("Car

created!");

}

}

class Car():

def destructor():

print "died"

public class Car

{

public ~Car()

{

Console.WriteLine("died");

}

}

raise NotImplementedException()

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");

}

try:

# do something

except e as SoapException:

print e.Detail

except e:

print e

try

{

// do something

}

catch(SoapException e)

{

Console.WriteLine(e);

}

catch(Exception e)

{

Console.WriteLine(e);

}

try:

# do something

except e:

print e

ensure:

print "done"

try

{

// do something

}

catch(Exception e)

{

Console.WriteLine(e);

}

finally

{

Console.WriteLine("done");

}

try:

# do something

except e:

print e

raise

try

{

// do something

}

catch(Exception e)

{

Console.WriteLine(e);

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) =>

Console.WriteLine("Clicked");

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

myList.Find(delegate(int i)

{

return i> 5;

});

namespace Foo.Bar.Baz

class Foobar:

pass

namespace Foo.Bar.Baz

{

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

*/

Useful macros

assert user is not null

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

not null"

print "foo"

Console.WriteLine("foo")

using db = RhinoConnection():

pass

using(RhinoConnectiondb = new

RhinoConnection())

{

}

lock syncLock:

#code under lock

lock(syncLock)

{

// code under lock

}

posted @ 2012-10-10 08:26  2012  阅读(786)  评论(0编辑  收藏  举报