Welcome to SpellCoder Sign in | Join | Help

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.

  • Extension Methods
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,
  1. The first option is to inherit the class and add the function.
  2. 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,
  1. Make sure that the class is static
  2. 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()

  • Anonyous Methods
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
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

kick it on DotNetKicks.com
Published Friday, December 08, 2006 1:12 AM by Mohammed Hossam
Filed Under: , ,

Comments

# re: C# 3.0 and LINQ, SilverKey Demo Day 1

Friday, December 08, 2006 1:32 PM by Chyld

# re: C# 3.0 and LINQ, SilverKey Demo Day 1

Friday, December 08, 2006 2:43 PM by Simone Busoli
Thanks Mohammed for the interesting presentation. It's the first time I read the principal new features of C# 3.0 exaustively and clearly exposed.

# re: C# 3.0 and LINQ, SilverKey Demo Day 1

Friday, December 08, 2006 3:34 PM by Adel
good article, i wished you had more complicated problems to be solved with the new enhancment.
Thanks.

# C# 3.0 and LINQ, SilverKey Demo Day 1

Thursday, January 04, 2007 5:55 PM by ALT-TAB Forums
Linked for good information - Forum Post.

# re: C# 3.0 and LINQ, SilverKey Demo Day 1

Tuesday, January 09, 2007 1:59 AM by Ahmed
Thank you for such a useful presentation.

# re: C# 3.0 and LINQ, SilverKey Demo Day 1

Sunday, April 22, 2007 10:50 AM by Erik
Hi Mohammed,

Nice presentation! I was wondering which tool you used to create the title headers in the presentation. They are nice!

Please let me know.

Regards,

Erik

# re: C# 3.0 and LINQ, SilverKey Demo Day 1

Monday, April 23, 2007 4:32 AM by Mohammed Hossam
@Erik
I didn't use any special fancy tools, just Microsoft Power Point 2007 Beta 2 TR (it is released now), and the title headers I just applied the Shadow effect on the text, you will find it in the same toolbar with the Bold, Italic & underline options .

# Entourage Scared Straight Megaupload, Entourage Fire Sale Rapidshare

# Volvo Xc70 Rebates Canada, Xc70 Car Parts Warehouse Compare

# V3500 Performance, V3500 Parts Combo

Thursday, May 20, 2010 1:18 PM by V3500 Performance, V3500 Parts Combo

# 09 Hyundai Sonata Accessories, Hyundai Sonata Headlight Catalytic Converter

# E 250 Super Duty Cooling System Explorer Sport Trac Cargo Van, Ford E 350 Econoline Discount Crown Victoria Super Duty

# Gx470 Second Hand, Gx470 Brake Disc Lexus Ls400

Thursday, May 20, 2010 11:36 PM by Gx470 Second Hand, Gx470 Brake Disc Lexus Ls400

# Cobra Xrs 9550, 550i Discount Auto Parts Brake Caliper

# Wholesale Ford P20, Carrs P20 Tool Steel

Friday, May 21, 2010 1:15 AM by Wholesale Ford P20, Carrs P20 Tool Steel

# Scrambler Adapter, 2005 Scrambler 500

Friday, May 21, 2010 2:21 AM by Scrambler Adapter, 2005 Scrambler 500

# Equinox Model Star Trek Voyager, Equinox Pot Minutes

# Volkswagen Phaeton Range, Cheap Volkswagen Phaeton Dealers

# Par20 Fluorescent Accent Lighting, Accent Accessories Bangalore Hyundai Santro

# 850ci Auto Bmw 650i, Accessories Bmw 850ci Brake Pads

Saturday, May 22, 2010 11:04 AM by 850ci Auto Bmw 650i, Accessories Bmw 850ci Brake Pads

# Mpeg Lss, Lss Discount Cal

Saturday, May 22, 2010 3:35 PM by Mpeg Lss, Lss Discount Cal

# Summit Manufacturers, Summit Insurance Services

Saturday, May 22, 2010 7:32 PM by Summit Manufacturers, Summit Insurance Services

# 250sl Automatic Transmission Inline Six, Part 230sl 250sl 280sl Pagoda

# 1976 Datsun 260z Sale, Headlight Datsun 240z 260z 280z Front Bumper Chrome Trim - 38.tvshowzone.com

# 2001 Grand Am Headlights Pontiac Montana, Montana Tea Spice - 390.luna-atra.net

# Glf Hard, Glf Power Need - 270.eumreborn.com

Monday, May 24, 2010 5:50 AM by Glf Hard, Glf Power Need - 270.eumreborn.com

# Gmc C32 Bulb, C32 Wholesale Mercedes Benz - 336.renters.ws

# Discount Flat Screen Televisions, 21 Televisions - 358.luna-atra.net

# 2002 Mazda Millenia Performance Parts Beck Arnley, Mazda Millenia Parts Accessories Timing Belt Tensioner - 487.jordanbrandallamerican.com

# 1994 Beretta Radiator Spark Plug Wires Chevy Corsica, Land For Sell Corsica - 320.tijuanareader.com

# Toyota Starlet Performance Celica Gt Four, Starlet Bulb Sylvania Silverstar Paseo - 229.an74.com

# 325es Parts Oem Bmw 325ix Brake Pads, Bmw 325ix Pt Discount - 23.zapstreaming.com

# C55 Car Parts Ml55 Clk63 Amg, Mercedes Benz Used Clk63 Clk55 Amg - 347.defutbolazo.com

# 2000 - 1999 @ 1988 Jeep Comanche Sale Autos, Lonesome Dove Discount Comanche Moon Karl Urban - 114.jordanbrandallamerican.com

# 2009 - 1980 @ P25 P2550 Minivan Boiler, P25 P2500 Car Part Comfort - 340.computeronlinebingo.com

# 1989 - 2000 @ Pontiac Grandville Second Hand Dash Panel, 1972 Pontiac Grandville Cheap - 238.animejin.com

# 1995 - 1983 @ 1969 Mercury Montego Truck Parts, Wholesale Body Parts Mitsubishi Montero - 261.tijuanareader.com

# 2000 - 1991 @ 2008 Gmc Sierra 1500 Denali Crew V8 Engine, Order P1500 - 494.binggreen.com

# 1989 - 2000 @ J200 Performance Chips, 1975 Jeep J20 Parts Rack & Pinion - 179.animejin.com

# 2000 - 1991 @ 2009 Chrysler Sebring Fuel Economy, Cabrio Parts 2008 Chrysler Sebring Convertible - 409.eumreborn.com

# 1989 - 2000 @ 600sl Blub, 600sl Wamp - 112.animejin.com

# 1981 - 2003 @ Episode Caliber Ford, Caliber Hawk Performance Hps - 108.luna-atra.net

# 1981 - 2007 @ 1993 Toyota Camry Wagon, 2008 Toyota Avalon Limited Sale - 277.renters.ws

# 1996 - 1988 @ Cupon Mercedes 560sel, Will Mercedes Benz 560sel Parts - 49.zapstreaming.com

# 1996 - 1988 @ African Safari Price Adventure, Safari Javascript Core - 153.zapstreaming.com

# 1990 - 2005 @ 323 Discount Millenia Parts Mazda B2300, Discounts Mazda Millenia Headlight - 210.an74.com

# 1989 - 2008 @ Celebrity Custom Homes Scottsdale Az, Celebrity Galapagos Cruise Review - 363.zapstreaming.com

# 2005 - 1992 @ 2005 F250 Pickup Parts F150, 1997 F250 Lift Kit - 328.myipgirl.com

# 1992 - 2001 @ 350sdl Wholesale Aftermarket, 350sd Full Download Gps - 242.mfbattle.com

# 1999 - 1998 @ Omega Deployant Clasp, Omega Seamaster Aqua Terra Quartz Uk - 303.tijuanareader.com

# 2002 - 1980 @ Gmc R3500 Replacement High Definition, R3500 Punk - 253.myipgirl.com

# 1997 - 1990 @ 2001 Gmc Sierra 1500 C3 V8 Engine, Radiator C3 Yukon Denali Xl Gmc Sierra 1500 - 274.ja3ra.com

# 1981 - 1996 @ Mazda Tribute Discount Review, 1993 Ford Probe Distributer - 76.luna-atra.net

# 2002 - 1980 @ Verona Specifications Exterior, Aerio Sell Trucks - 270.binggreen.com

Anonymous comments are disabled