Jsbi

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 12 May 2010

New Features in C# 4.0

Posted on 00:06 by Unknown
Visual Studio 2010 is out and we have a new version of C# that also accompanies it. That's C# 4.0 - I have been working on C# from the good old 1.0 or 1.1 version upwards. So whats the new 4.0 version of C# got in store for you. Here's a wrap up of the new features available as part of C# 4.0 release.

keyword Dynamic

There's a new kid in the block, with the introduction of a very a key feature of this release called the dynamic keyword. The keyword dynamic bridges the gap that existed between a dynamically and statically - typed languages. Well, C# can now also be considered a dynamically typed language too. You can easily create dynamic objects on the fly and even have their types determined only at run time. You will simply need to add a new namespace called System.Dynamic, and lo you can create expandable objects and even advanced class wrappers. You can also facilitate interoperability between different languages, including the dynamic ones.

To better understand the use of dynamic keyword, here's an example:

dynamic BankAccount = new ExpandoObject();
BankAccount.Number = "174258733-451269";
BankAccount.HolderName = "Jason S";
BankAccount.Branch = "Bangalore";

Though there are many pros and cons for the use of dynamic keyword and the system performance overheads, it makes like that much more simpler for the developer in the end.

Optional (or Default) Method Parameters

If I rightly remember, VB.net is preferred to C# when porting old projects in ancient langues of VB 5 / 6 or when working on creating COM wrappers since VB.net allows you the use of Optional or Default parameters in methods. C# 4.0 has finally got this feature to make it that much more robust in this area.

You can now easily specify a default value for a parameter within the method declaration in C# 4.0. Also, the consumer of the method can either pass a value or can easily skip the parameter. When the parameter is skipped, the default value declared in the method is passed.

Here's a simple example to explain it:
Method declaration:
public static void GetMoney(int amount = 0) { }

Bot the below Method calls for the above declaration are valid in C# 4.0:

GetMoney(); // 0 is used as amount in the method.
GetMoney(1000);

Named Arguments

Well, here's another relief to the developer. Did you know that in C# 4.0 the order of parameters in a method declaration and also the order of arguments you pass to the method when calling don’t need to match anymore!!. You can now easily provide arguments to a method in any order that you are comfortable with by specifying parameter names with-in the method call. This feature also tremendously improves the readability of one's code.

Here's a simple example of using Named Arguments in C# 4.0
var sample = new List();
sample.InsertRange(collection: new List(), index: 0);
sample.InsertRange(index: 0, collection: new List()); // both ways it'll work now

Covariance and Contravariance

Does the heading sound confusing? Well, the use of variance on generic type parameters in interfaces and delegates is yet another new feature available in C# 4.0. Though, strictly speaking it doesn’t add that much of a new functionality. But it rather makes things work in the first place as you expected them to work. Here is a major advantage that is hidden in the power of C# 4.0 in simple line below. Note: The below code wouldn't have compiled up until C# 4.0 was released:

IEnumerable objects = new List();
Now you have the ability to implicitly convert references for objects instantiated with different type arguments. This makes it that much more simpler and easier to reuse code.

Improved COM Interop Support

As I started this article with the dynamic keyword, then we had the optional parameters and named arguments, did you know that all of this actually enables a significant improvement in working with COM interop. Though all these features have arrived pretty late for the COM Interop world - but it's like late than never is better.

Take a look at my ugly code below:

var excelApp = new Excel.Application();
// . . .
excelApp.get_Range("A1", "B4").AutoFormat(
Excel.XlRangeAutoFormat.xlRangeAutoFormatTable3,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
I can now simply write it in a few lines as below:

excelApp.Range["A1", "B3"].AutoFormat(
Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);

Ain't it pretty neat?

So, there you go - that was a wrap up of the new features that are available in C# 4.0. Hope it helped!!
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in microsoft, programming, software, Tips | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • 100+ wishes on Orkut + Facebook for your Birthday!!
    Well, first of all thanks to all those numerous mails that I received from you all yesterday - it was my birthday yesterday. Unfortunately m...
  • Comodo Firewall :: A really good one
    As internet is becoming common in our house-holds so is the virus and trojan attacks. The best way to protect yourself from the Trojan warfa...
  • Complete list of Orkut Country Specific Websites – use these when Orkut is blocked to scrap
    Orkut – the popular social networking website from the Google stable has many country specific websites. Though most of us are only aware ...
  • www.kseeb.org website is down already
    When everyone is waiting anxiously for the SSLC results we have one shocker - the website where the results are to be announced is down!! Ye...
  • The Tech Evolution : 1GB memory 20 years ago and 1GB memory now
    The Intel Museum , which is located in Santa Clara, California, USA had recently introduced several new exhibits that demonstrate the transf...
  • HP PhotoSmart Free Printer Giveaway contest - Day 3 of 7
    Well, well - Here we have the day 3 of the contest for you. There was a barrage of e-mails yesterday as to what happened to the contest ques...
  • It's been long since I wrote
    Sorry Folks, it's been quite some time since I wrote - a week to be precise. Well - here goes the lame excuse. I was busy with assemblin...
  • Can your OS make some ice-cream for you? Well, Linux can!!
    We have the OS wars raging like the Cold War down under and ready to erupt any given time. In this time, I have a question for you - Can you...
  • Get 2nd PUC results on SMS for May 2012
    The Karnataka 2nd PU results for 2012 can be checked on the internet by following :  Get 2nd PUC results on SMS for May 2012 But did you kno...
  • Now watch videos on YouTube in 720p High Definition (HD) quality for Free
    YouTube recently launched an exclusive High Definition section for the US users. Users who have access to this section can watch the videos...

Categories

  • Android
  • announcements
  • Anti-Virus
  • Asus
  • Bangalore
  • blogging
  • Contests
  • FaceBook
  • Gadgets
  • google
  • Guest Article
  • health
  • HP
  • India
  • internet
  • iPhone
  • K810i
  • linux
  • mac
  • microsoft
  • Mobile
  • MotherBoards
  • mp3
  • NetBook
  • news
  • Office
  • personal
  • photoshop
  • Portable Apps
  • Printers
  • programming
  • Quotes
  • Review
  • software
  • story
  • technology
  • Tips
  • Travel
  • videos
  • vista
  • windows
  • Windows 7
  • Windows 8
  • zune

Blog Archive

  • ►  2012 (8)
    • ►  October (2)
    • ►  May (6)
  • ►  2011 (1)
    • ►  December (1)
  • ▼  2010 (22)
    • ►  July (1)
    • ▼  May (16)
      • ICSE Results to be declared today on www.cisce.org
      • Finally we have a Steam client for Mac for FREE fr...
      • New Features in C# 4.0
      • Can your OS make some ice-cream for you? Well, Lin...
      • FaceBook Login
      • A few FaceBook Login Tips
      • List of Best Engineering Colleges in Bangalore : R...
      • Check your COMEDK 2010 (UGET) results online : Com...
      • Check your CET 2010 results online : Complete list...
      • We now have Intel Atom processors to power Tablets...
      • Complete list of Best and Free Anti-Virus for your...
      • How to disable Microsoft Windows Genuine Advantage...
      • II PU (PUC) 2010 results announced : Website to ge...
      • www.kseeb.org website is down already
      • Karnataka SSLC 2010 results Online :: Website Anno...
      • How to buy gold bullion and also steps to safeguar...
    • ►  April (1)
    • ►  March (2)
    • ►  February (1)
    • ►  January (1)
  • ►  2009 (167)
    • ►  December (3)
    • ►  November (19)
    • ►  October (12)
    • ►  September (8)
    • ►  August (11)
    • ►  July (22)
    • ►  June (30)
    • ►  May (26)
    • ►  April (20)
    • ►  March (12)
    • ►  February (4)
  • ►  2008 (102)
    • ►  December (3)
    • ►  November (1)
    • ►  October (10)
    • ►  September (17)
    • ►  August (16)
    • ►  July (5)
    • ►  June (7)
    • ►  May (10)
    • ►  April (18)
    • ►  March (15)
Powered by Blogger.

About Me

Unknown
View my complete profile