Gypsy女郎的大秘密











“A lambda expression is written as a parameter list, followed by the => token, followed by an expression or a statement block.”

This is not the first time I see Lambda expressions. I love Lambda expressions in the first sight because the look pretty much like logic to me. Alex talked about this in his LINQ example. I will talk about it in LINQ later, but here are some examples I copy from c#3.0 specification document:

x => x + 1 // Implicitly typed, expression body
x => { return x + 1; } // Implicitly typed, statement body
(int x) => x + 1 // Explicitly typed, expression body
(int x) => { return x + 1; } // Explicitly typed, statement body
(x, y) => x * y // Multiple parameters
() => Console.WriteLine() // No parameters

Honestly I am not confident in typing an example for this in notepad and assume it will work. So i will go home and install VS 2008 beta on my machine first, have a try and create an example. Then I can create example for LINQ as well.:)



{九月 12, 2007}   C# 3.0 Basic – Anonymous type

“An anonymous object initializer declares an anonymous type and returns an instance of that type.”

Cool, another thing in .NET allows anonymous. It’s quite handy to have anonymous method and type support.

var meeting = new {Time = 3.Hours().Ago(), Location = “Ellerslie”};

The variable meeting has a type, which, technically not really anonymous. An ugly system name is assigned to this type and which I honestly cannot remember.

If there is another anonymous type with the same initializer, C# 3.0 is smart enough to recognise them as the same type. e.g.

var meeting2 = new {Time = 3.Hours().FromNow(), Location = “CBD”};
meeting = meeting2;

I think this is really cool although I don’t have in mind where can I use this yet, haha.



Alex’s example on extension method as a whole was really cool. I will ask him for it later. Meanwhile I will write a super simplified one to show that I understand what he was saying.:D

First here are two extension methods. The first one, Hours, extends int type; and the second one, FromNow, extends TimeSpan type.

public static TimeSpan Hours(this int i)
{
return new TimeSpan(0, i, 0, 0);
}

public static DateTime FromNow(this TimeSpan t)
{
return DateTime.Now.Add(t);
}

So I can write something like this:

DateTime ThreeHoursFromNow = 3.Hours().FromNow();

Probably my example cannot illustrate the power of extension methods, but well, you get the idea. :)



{九月 12, 2007}   C# 3.0 Basic – Implicit typing
var i = 5;
var s = “Hello”;
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var persons= new Dictionary<int,Order>();

Basically the above are equivalent to

int i = 5;
string s = “Hello”;
double d = 1.0;
int[] numbers = new int[] {1, 2, 3};
Dictionary<int,Order> persons= new Dictionary<int,Order>();

“In an implicitly typed local variable declaration, the type of the local variable being declared is inferred from the expression used to initialize the variable”. IntelliSense recognises the type of the variable and support normal auto completion of that type for the variable.

Note that the following are example of incorrect declarations:

var x; // Error, no initializer to infer type from
var y = {1, 2, 3}; // Error, collection initializer not permitted
var z = null; // Error, null type not permitted


According to C# 3.0 specification, “a collection initializer consists of a sequence of element initializers, enclosed by { and } tokens and separated by commas”.

So

Person p1 = new Person(“Jacqualine”, “Chow”);
Person p2 = new Person(“Andre”, “Meurer”);
Person p3 = new Person(“Alex”, “James”);

List<Person> pList = new List<Person>();
pList.Add(p1);
pList.Add(p2);
pList.Add(p3);

can be written as

List<Person> pList = new List<Person>{ p1, p2, p3};

and in combining the power of object initializer, this will become

List<Person> pList = new List<Person>
{
new Person { Firstname = “Jacqualine”, Lastname = “Chow” },
new Person { Firstname = “Andre”, Lastname = “Meurer” },
new Person { Firstname = “Alex”, Lastname = “James” }
};


This is the first thing I remember from Alex’s talk.

According to C# 3.0 specification, “an object initializer consists of a sequence of member initializers, enclosed by { and } tokens and separated by commas”.

So

Person p = new Person();
p.Firstname = “Jacqualine”;
p.Lastname = “Chow”;

and

public Person(string firstname, string lastname)
{
this.Firstname = firstname;
this.Lastname = lastname;
}

Person p = new Person(“Jacqualine”, “Chow”);

can be written as

Person p = new Person { Firstname = “Jacqualine”, Lastname = “Chow” };

In this case we do not have to create a lot of constructors for different combination of initial parameters.



et cetera
Follow

Get every new post delivered to your Inbox.