SQL Server knowledge center

everything about SQL Server
See also: Other Geeks@INDC

C# 3.0 new language features

C# 3.0 new language features

By  : Kasim Wirama, MCDBA, MVP SQL Server

 

.NET 3.5 has been launched in November 2007. I read and tried new features of C# language (code name : Orcas) with Visual Studio 2008. These are new features for C# 3.0 in .NET framework 3.5 :

 
  1. Local type inference

You can declare your variable in more relaxed way, while preserving type safety on design time rather in run time.

 

For example you declare this variable below

int i = 5;

 

you can write as below :

var i = 5;

 

Compiler will know that variable i has type integer.

 
  1. Lambda expression

You can define anonymous method with more flexible syntax, it is other alternative to delegate use.

 

For example when you express in delegate :

 

Sum = Aggregate ( l.values, delegate( int a, int b) {return  a + b;});

 

With lambda expression you can replace as:

Sum = Aggregate ( l.values, (int a, int b) => {return a + b;} );

 

It is called explicitly type parameter list.

 

You can simplify it further :

Sum = Aggregate (l.values, (a,b) => {return a + b;});

 

It is called implicitly type parameter list.

 

You can simplify it more further :

 

Sum = Aggregate (l.values, (a,b) => a + b);

 

You can read : given a and b, return a + b

 
  1. Extension method

You can extend new method to instance of any class either FCL classes or custom classes.

 

For example  you can extend your own method to string instance.

 
  1. Object Initialization expression

You can initialize object and assign value to its parameters with single line of code.

 
  1.  Anonymous type

You can create anonymous class with its properties and values.

 
  1. Query expression

This is LINQ query expression.

Here is the example :

var customers = new []{

    new {  Name = "anto", age = 15 },

    new {  Name = "sella", age = 20 },

    new {  Name = "rudi", age = 30 }

};

 

You can get customer list that has age over 18 by expressing this query expression :

 

Var expr = from c in customers

                        Where c.age > 18

                        Select c.name;

Foreach(var x in expr)

{

            Console.WriteLine(x);

}

 

Let’s try it to get some exciting experience with these cool features.

 
Share this post: | | | |
Posted: Nov 27 2007, 11:39 PM by Kasim.Wirama | with 9 comment(s)
Filed under:

Comments

Haryo Prabowo said:

wee, I am about to write the same topic,

but looks like someone catch the ball first =D

Well, maybe next time I'll post about the new Visual Studio 2008 features. Not about C# in particular though, but it cover some common new features such as LINQ, etc ...

* I'm currently writing an article for MS National Writing Competition 2007.. But I think it won't be good to publish it before the NWC contest ended, so maybe it will take some time before I can do the post.

# November 28, 2007 7:40 AM

irwansyah said:

Hahahaha...C# 3.0 niru JavaScript abis!!!!

# November 28, 2007 10:01 AM

Bebin said:

Apakah Lambda Expression secara keseluruhan lebih fleksibel daripada LINQ?

Gw sih masih cupu ttg LINQ, tapi g blom nemu cara Select-Single dari LINQ 2 Objects. Kalo pake Lambda Expression sih bisa :)

# November 28, 2007 4:17 PM

TrackBack said:

# November 29, 2007 4:42 AM

Kasim.Wirama said:

apakah lambda expression secara keseluruhan lebih fleksibel daripada LINQ? rasanya iya, kenapa saya katakan iya? karena lambda expression digunakan untuk LINQ, expression tree maupun dalam predicate expression.

Itu menurutku dari sisi fungsionalitas, untuk sisi usability di dalam code, aku belum menemukan jawaban atas pertanyaan itu. :)

# November 29, 2007 10:02 AM

Kasim Wirama said:

Kenapa niru javascript abis? Setahuku memang ada keyword yg mirip javascript seperti var. Namun reason dan objective dari pada itu, aku yakin pasti berbeda arsitektur object model.

# November 29, 2007 10:09 AM

Kasim Wirama said:

Haryo,

It is good you write for .NET stuff. Anyway, LINQ itu big topic, may be you can write LINQ to XML, etc.

I wish you good luck for MS National Writing Competition 2007.

# November 29, 2007 10:11 AM

Kasim Wirama said:

Ternyata ramai juga comment untuk artikel ini. Thanks a lot untuk Haryo, Irwansyah, Bebin, dll atas commentnya. :)

# November 29, 2007 10:15 AM

Bayu said:

Weee..... belajar yg versi 2.0 aja lom kelar, udah nongol versi 3.0.

# February 12, 2008 5:20 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 
Are you human?:  


Enter the numbers above: