More Impressions of C#: 3.0

The topic of this post is basically: lambdas are awesome.

C# 3.0 introduced lambda expressions with type inference and a set of generic delegates in the .NET framework.  That means you can do this:

event Action<string> MyEvent;

MyClass.MyEvent += (s) => { doStuff(s, 5) };

The => operator defines an anonymous method (lambda) taking the arguments on the left to the statements or expression on the right.  Types can be specified, but don’t have to be — the compiler can infer them, in this case from the event type.  Note that it takes exactly one simple short line to define an event, and one simple short line to connect it.  There is no redundancy or boilerplate code, and the function handling the event does not have to be written specifically for that purpose.  Sweet.

The above is roughly equivalent to, in PyQt:

self.emit(SIGNAL(“myEvent”),”foo”)

self.connect(someObj, SIGNAL(“myEvent”), lambda s: doStuff(s, 5))

In Qt with C++, you would have to define an extra method if you just wanted to pass an extra argument like this.  There are of course many other uses for lambdas.  One new use in C# 3.0/.NET 3.5 is the LINQ framework, which defines extension methods for common types, in particular IEnumerables that, in the simplest use, allow you to use lambda expressions to execute queries:

IEnumerable FilteredList = MyList.Where(person => person.Name == “bob”);

LINQ also allows you to write these queries using an SQL-like syntax.

The hundreds of other posts and documents online can explain it all better than I can, but finding things like this makes me like C# more and more.

About these ads

Tags: ,

3 Responses to “More Impressions of C#: 3.0”

  1. Jared Spurbeck Says:

    Cool. ^.^

    Know of any good tutorials for learning C# that’d be useful to an aspiring Mono developer? All the ones I’m finding are Microsoft-centric!

    • Yuriy Says:

      I’ve actually been mostly working on Windows, with only a little dabble into Mono. Most resources are Microsoft-centric, but that shouldn’t stop you from using them. If you don’t use third party libraries or WPF, most code you write in C#.NET will run on Mono. If you are looking for information on the non-MS GUI toolkits available for Mono, you are mostly best off looking at those toolkits’ (GTK, Qt) original documentation, and there is at least some sparse information around on how to use the bindings. You should definitely start by installing Monodevelop, which works well with GTK#.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: