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: | | | |
Published Tuesday, November 27, 2007 11:39 PM by Kasim.Wirama
Filed under:

Comments

# re: C# 3.0 new language features

Wednesday, November 28, 2007 7:40 AM by Haryo Prabowo

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.

# re: C# 3.0 new language features

Wednesday, November 28, 2007 10:01 AM by irwansyah

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

# re: C# 3.0 new language features

Wednesday, November 28, 2007 4:17 PM by Bebin

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 :)

# http://us.mg1.mail.yahoo.com/dc/launch?.rand=d0qknspfckpht

Thursday, November 29, 2007 4:42 AM by TrackBack

# re: C# 3.0 new language features

Thursday, November 29, 2007 10:02 AM by Kasim.Wirama

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. :)

# re: C# 3.0 new language features

Thursday, November 29, 2007 10:09 AM by Kasim Wirama

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.

# re: C# 3.0 new language features

Thursday, November 29, 2007 10:11 AM by Kasim Wirama

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.

# re: C# 3.0 new language features

Thursday, November 29, 2007 10:15 AM by Kasim Wirama

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

# re: C# 3.0 new language features

Tuesday, February 12, 2008 5:20 PM by Bayu

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

Powered by Community Server (Commercial Edition), by Telligent Systems