C# 3.0 language features

The language is made smart, and I can’t resist also pointing out a new ingenious language feature of C# 3.0 introduced to me at MSDN Live.

var result = new {myText="some string", myInteger=12345, myClass=new MyClass()}

The code snippet will create an instance with these three properties, implicitly declared public as a string, an integer and an object of type MyClass. Of course, Visual Studio 2008 provides auto completion for accessing these properties.

string theText = result.myText
MyClass myClass = result.myClass

It solves neatly the problem of temporary storage of data in a method. You won’t have to declare any more temporary class to store the data. Sweet! :)

Read this article for a preview of what more to come with C#3.0. By the way, wonder why they had to make the version numbers of .Net and C# so confusing? To use C# 3.0, you need .Net 3.5, not .Net 3.0. Oh well. It ships with Visual Studio 2008)

About Ole Morten Amundsen

Developer, programmer, entrepreneur. Java, .Net, ruby, rails, agile, lean. Opinionated enthusiast!
This entry was posted in .Net. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s