Månad: april 2008

With Linq came… Part two

Lambda Expression

Lambda Expression is a kind of syntax shortcut for a anonymous method, the operator for Lambda is => and should be read as goto, the operator separates the inparameter on the left side and the lambda body on the right side. A sample of an lambda expression:

int[] arrInt = {1,2,3,4,5,6,7,8,9,10};

//Lambda Expression
var aa = arrInt.Where(s => s % 2 == 0);

foreach (var item in aa)
{
    Console.WriteLine("{0}", item);
}

This code will write out every even number in the int array arrInt. From the begining lambda expression was the syntax to be used with Linq, but when the Developer team let diffrent kind of users test this, it was considered as not good enough. Query expression was the solution for that, with this you can write the same thing this way:

int[] arrInt = {1,2,3,4,5,6,7,8,9,10};

//Query Expression
var aa = from s in arrInt
where s % 2 == 0
select s;

foreach (var item in aa)
{
    Console.WriteLine("{0}", item);
}

This will do exact the same thing, query expression syntax is more readable then lambda, lambda is nice when the query is very simple. But where did the Where method came from you may ask does int[] array now have a Where method? Next subject will explain this.

Extension Methods

Look at this screenshoot:

Intellisense says it all, those methods that has the symbol is an extension method and not really a part of the int array class. Many extension methods has been added by the Microsoft developer team to arrays and collections to be used with Linq. But you can also add extension methods yourself. I show you how, here a funny example in a simple console application I want to extent the string class and add a method called ToVoice().

You can download the project here
When you run the method ToVoice() a synthetic voice will say in this case "painkiller" 🙂
A simple example but using extension is easy, notice that the first parameter uses the this keyword to tell the compiler wich target type the method is to be used for.

With Linq came… Part one

What´s the news in C# 3.0? Well the big news is Linq and the evolution of C# that the Linq project forced the C# developer team to implement. This new cool stuff will make C# more powerfull even if you dont use Linq. So what is the new stuff?

Query Expression

It´s so awesome, I remember when I first read about SQL 2005, rumors where saying that you should be enabled to write Stored procedures with the C# language. How disappointed I was when SQL 2005 was released, yes you could write C# and let SQL host the assembly but this was no news realy you could not write real Querys with the power of TSQL. With Query Expression this is possible, it has even more power then TSQL itself take a look at this simple example.

var query = from c in customers
where c.City ==  "Stockholm"
orderby c.ContactName.Split(‘ ‘)[1]
select c;

It is very simple query but notice how easy it is to use a C# string functions to sort on contactname (contact name is first name + last name), it´s just uses the Split and Array functionallity. You could do that in TSQL but not with the help of any .NET language.
This query expression works on any Collection in .NET that implements IEnumerable (and all .NET collection does) and you could of course create your own.
What is var you may ask and how do I use this? To use the simplest way just do like this:

foreach (var item in query)
{
    Console.WriteLine(item.ContactName);
}

It is when you start iterating the query realy fires, not in the select c statement.

C# 3.0 feature: ‘var’ Local Variable Inference

var is a new keyword and when you first se how it works you may think this is something taken from Visual Basic Language who has a Variant datatype. But it´s not (and thats good VB is pure evil) it is strongly typed and it only works in a local scope. Here is an Example:

//C# 3.0 feature: Object and Collection Initializers in a mix

//C# 3.0 feature: ‘var’ Local Variable Inference (strong typed, no object or variant list it is a List<Customer>)
//Local Variable Inference only works on variables in local scope
var list = new List<Customer>
{
    new Customer(){CustomerId =  "FRAERI", City = "Stockholm", ContactName = "Frasse Ericsson"}
    new Customer(){CustomerId =  "BOSAND", City = "Stockholm", ContactName = "Bosse Andersson"}
}

The code also show two other new features Object and Collection Initalizers here mixed all together. The var datatype will be a List<Customer> and even in design time the Intellisense will show that. By inference the compiler will understand what the datatype must be, this is usefull in local scope, you can write more compact code. The C# team have done som more improvements to write less code, more samples to come…

History of C# and some confusion added by Microsoft

A little history to clear up the version mess that Microsoft created when releasing .NET Framework 3.0, they should have named it 2.5 but because of this the C# language version and the Framework version is not the same. The list below describes the diffrent releases of .NET Framework and the C# language version (only major releases).
 
  • .NET Framework 1.0 C# Language Version 1 released 2002 with "Visual Studio NET"
  • .NET Framework 1.1 C# Language Version 1 released 2003 with "Visual Studio 2003"
    No bigger changes in this release just som improvements and fixes
  • .NET Framework 2.0 C# Language Version 2 released 2005 with "Visual Studio 2005"
    Generics and anonymous methods inplemented in the C# language this is a major improvement, the "Visual Studio 2005" is also much better, huge of ASP.NET improvments.
  • .NET Framework 3.0 Language C# Version 2.0
    No new version of Visual studio released, this version is a big addon for the 2.0 Framework but the name implies something else (my opinion). The new stuff that´s added is Windows communication foundation, Windows Presenation Foundation, Windows Workflow Foundation and something called Windows Cardspace. 
  • .NET Framework 3.5 Language C# Version 3.0 released 2007 with "Visual Studio 2008"
    Linq is introduced and with it a lot of new cool stuff that I will show later on in this blog, Linq is a little revolution of it´s own. I´v have dreamed about writing querys in SQL style against collections, Arrays, XML, SQL databases or DataSets but use the full potential of the C# language and now it´s reality.
 

Welcome to a new C# Blog

I am an entusiastic C# programmer and I will for now on stop torture my non programmer friends with blogs on the subject C#, finally I´v realized that they are not interested. This day I´ve only played around with the modules and other cool stuff that I can do. When I am using this I got a Sharepoint feeling, is´nt windows live spaces just a big Sharepoint installation? Well tomorow I will start filling this site with som cool C# content…