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

  • Making of Vodafone ZooZoo Ads : Exclusive Images from behind the scenes
    Most of you have become fans of ZooZoo - the non-animated character created by O&M for the latest batch of Vodafone Ads. The last time ...
  • Why should NFC be switched off when not in use?
    NFC or Near Field Communication that you find in almost all modern smartphones except Apple products is a set of standards for smartphones a...
  • Prices in India for the New Apple MacBooks
    The prices of the new Apple MacBooks have been released in India too. Though the machines themselves will be released in India only later th...
  • How to create a very successful CBT (Computer Based Training)
    Today I'll touch upon the topic of how to create a very successful CBT (Computer Based Training) program. Usually a computer based train...
  • Technology can help you take care of your elderly Parents easily!!
    With the advent of technology - it has begun to affect our lives in many facets. Now getting a Medical Alert has also become very easy and ...
  • Ribbon to be natively supported in Windows 7
    The people who are drafting the new user interface guidelines for Windows 7 plan to build the Ribbon , Jewel and “other new UI concepts” nat...
  • Easy tips to Working smarter and Not harder
    The old saying goes as " Working smarter is not necessarily harder ". Today I'll tell you all the secrets on how you can maste...
  • Why do you think thermal runaway reactions are dangerous on an industrial scale?
    Thermal runaway reactions are always said to be dangerous on an industrial scale since the reactions go faster in an industry where they ten...
  • Adobe Finally enters the Online Application Market
    Though a bit late - but Adobe seems to be in a state of "Better Late than Never" and has rolled out an online website at Acrobat ....
  • Winner of the HP PhotoSmart Printer Giveaway
    Well, folks - it wasn't that easy to choose a winner. There were 340 correct / qualifying entries to the contest. That was a bit too les...

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