C# 3.0 and LINQ, SilverKey Demo Day 1
I'm sorry for being late, I should have written this really long time ago, since the
SKDD1 last November, but I was really busy the last couple of weeks.
as a good apology of this I'll try to do the presentation I did in SKDD1 here in my blogs with description, and also the PPT and all the samples of C# & LINQ are attached at the end of this blog post.
If you want the complete material,
Click Here to download
So let's start


So, today I'll be talking about one of the future technologies which is supposed to be available next year with the release of Visual Studio 2007 (ORCAS) and .net Framework 3.5, which sounds really far, but this is really a huge step for programming languages in general and especially .net programming, which we should be prepared for it.
The presentation today will be divided into two main parts
- Part 1, C# 3.0 Language Enhancements
Which we will talk about what's new in C# 3.0 as a language, and compare between the different versions of C# - Part 2, Language Integrated Queries (LINQ) Project
Which is the coolest thing C# will have, and I personally consider it as a huge step for programming in general.
So now starting with the first part

- When we was first introduced to C# 1.x in 2002, we were in the Catch-up Phase of C#, which we were introduced to concepts which were originally introduced in some languages such as Java, Smalltalk, C++ .. etc as you can see in the Blue Bubbles
- Then with the next major version of C# 2.0 in 2005, we were in the Advance Phase, when C# was really moving beyond expectations that C# introduced some concepts from other functional languages such as anonymous methods, Iterators, as well as some other features like Generics which was really a great advance for C#. as you can see in the Orange Bubbles.
- So, Personally I didn't expect that C# can go further than C# 2.0 at least for more 5 years, but I was shocked that we start hearing about C# 3.0 even before the official release of C# 2.0, which was a sign that C# is not competing with similar languages in the market, but it is making a difference in the whole programming languages field, so I think we can say that C# is going to the Uniqueness Phase.

What is the problem that C# 3.0 is trying to solve in the first place ?
Actually they are more than one problem, but basically there are two main problems.
- Since long time ago, we had two big schools in programming languages, Static Languages & Dynamic Languages, which every school has its own philosophy about how to program.
C# 3.0 is trying to narrow this gap, it tries to borrow some concepts and features from dynamic languages which can be applied easily with static languages, stuff like Lambda Expressions, Local Variable Type Inference and Object & Collection initializers are easy to be added to static language without changing the shape of the language itself. - The other problem, when the development in databases and programming languages was separated, so when the databases moved to Relational Model, programming languages moved to hierarchical way of thinking in the form of Object Oriented Programming, which creates another gap between the Data & Objects, which C# 3.0 is trying to narrow.

Now we know why C# 3.0 is coming, so what C# 3.0 offers to solve these problems.
basically C# 3.0 is offering about Seven new stuff which six of them are listed in the above stuff, and the seventh which is LINQ, is discussed in details in the second part.

- Local Variable Type Inference
The first new feature we are talking about today, is Local Variable Type Inference, which is simply how to make the compiler smarter enough to infere (detect) the data type of the local function variables from the value you are assigning.
In other words, in the above example, you don't need to mention explicitly that
intVar is of type
int, because you are assigning the variable a value of type int which is
10, so simply you can declare all local variable using the
var keyword, and the compiler in compile time will replace each var keyword with the type of the value assigned to the variable, so in the previous example
var intVar = 10
will be compiled to
int intVar = 10
which helps to write less especially if you are writing a long type name, and also sometimes you don't really care about the type name especially in the
foreach loops.

This is the second feature of C# 3.0.
First let's consider the case we have a class, and we want to add extra function to it, so simply if we have the class code, we will edit that code and that's it, so what if this class is inside an assembly (dll) which we don't have the code, the solution will be one of two options,
- The first option is to inherit the class and add the function.
- The second option is to write the method we want to extend in a new class as a static method that takes an object of the class type and return a new object with the modification we want,
The first option sounds simpler, but actually it is harder, because sometimes you can't do this, because inheritance has some limitations, like you can't inherit a sealed class, or a primitive data type such as int, float, ... etc.
Ok, we still have the second option, which is writing a class anywhere, and add the function to it as static function. However, you can't do something like this
string x = "some string value";
string y = x.Reverse()
which you can't use the function as if it is part of the class itself, and you will end up with code similar to this
string x = "some string value";
string y = Util.Reverse(x);
that's because you wrote the Reverse function the Util function.
in C# 3.0 you can configure the Util.Reverse() function to make the first code doable, so consider the following code of Util.
static class Util
{
public static string Reverse(this string input)
{
char[] reversedChars = new char[input.Length];
for (int i = input.Length - 1, j = 0; i >= 0; --i, j++)
{
reversedChars[j] = input[i ];
}
return new String(reversedChars);
}
}
as you can see it is a simple static class, with one static function called Reverse, all what we have to do,
- Make sure that the class is static
- Add this keyword before the type of the argument you want to extend.
so now you can write this code,
string x = "some string value";
string y = x.Reverse()

This feature is simply, creating a complex type on the fly, which helps sometimes when you want a temp variable of complex type, that you'll never use again, so you don't need to write a class for it, so you just tell the compiler to write it for you, as you can see in the slide, you use the
var keyword to refer to the variable, and initailize by specifying the properties of the complex type and in the same time initializing them, so the compiler can also infere the type of the properties so the compiler can generate the class for you.

- Object & Collection Initializers
This feature is one of the features that already exist in most functional and dynamic langauges, which is initiating the object and in the same time assigning some value to the public properties of the object itself.
so in the above code, consider have a class called Point, with two properties X and Y, so simply we can initailze the object as shown in the first box, but in C# 3.0 we can write it like this
Point p1 = new Point {X=10};
Point p2 = new Point {Y = 10, X=20};
Lambda Expressions is one of most common features between dynamic and functional languages such as Lisp & Python .. etc.
Lambda Expressions is an improvement of the delegate idea, which improved in C# 2.0 as Anonymous Methods, and now in C# 3.0 it is Lambda Expressions.
Consider the following code, we declared a delegate called NumericalOperation which refers to a function that accepts two inputs of type int, and returns output of type int, and int first lines in the main, we are using C# 1.x code to use this delegate, so we create an object of the NumericalOperation that refers to the Add function, and then call the function using the delegate.
then in the next lines we are using C# 2.0 code to declare the object and in the same time declare the function using the anonymous method style, so we created a function that subtracts two integers, and use it .
then in the next lines we are using C# 3.0 code to declare the object and in the same time declaring the function using the lambda expressions code, which is somehow looks weird in the first time, but it is really easy to understand
first we write the delegate type followed by the object name on the left hand side, then on the right hand side we list all the input parameters that will be used in the expression in a coma list, then the arrow operator
=> followed by the expression that evaluates the return value.
so if you want to write a lambda expression that adds two numbers, so you can say it like this
NumericalOperation op = (x, y) => x + y;
delegate int NumericalOperation(int x, int y);
internal static void Main()
{
// C# 1.0
NumericalOperation add = new NumericalOperation(Add);
Console.WriteLine("C# 1.0: delegates {0,28}", add(1, 2));
//C# 2.0
NumericalOperation sub = delegate(int x, int y)
{
return x - y;
};
Console.WriteLine("C# 2.0: Anonymous Methods {0,20}", sub(2, 1));
//C# 3.0
NumericalOperation mul1 = ( x, y)=> x * y;
Console.WriteLine("C# 3.0: Lambda Expressions {0,20}", mul1(4, 5));
//Another Approach without the need to write the delegate
Func<int, int, int> mul2 = (x, y)=>x*y;
Console.WriteLine("C# 3.0: Lambda Expressions {0,20}", mul2(4, 5));
}
internal static int Add(int x, int y)
{
return x + y;
}

I have a blog post that I wrote before about
Expressions Tree, you can refer to it
So, I think now I covered most of what I said in the first session in SKDD1, soon I'll post the second session which is LINQ Project, keep in touch, and if you have any comments or question just add them as a comment to this post.
Thanks