Thursday, February 7, 2008

C# 3.5 - Properties Shorthand

I must tell you if today your C# knowledge is not up to date then you might face tough time with future versions of the language. Microsoft developers will keep on adding new things ranging from entirely new features like LINQ to something like Implicit types or Object initializations. Last two features that I've mentioned are due to LINQ and secondly they provide some kind of syntactical sugar for developers. In future, VB.NET and C# will take entirely different paths. C# will become more performance oriented and VB.NET more developer friendly. VB.NET will target UI where as C# will mostly be used as class libraries. I am not sure when will this happen but it will happen soon [trust my Microsoft contacts :) ]

Let me show you how property shorthand feature of C# works from inside. We will start of with a simple console application and add code as needed.

Adding property shorthand to your code
Type prop in code window and let intellisense jump in. Then select "prop" and press Tab 2 times. A property shorthand will appear
.

Complete your class definition
Add 3 shorthand properties and one normal property. Your final class definition will look like this



















Create a class that will use Customer object.
Main() method in Program.cs would be enough for us
,













And the output is as expected,




Let us examine the IL that is generated for this managed assembly. Below are two images one with IL showing members of Customer class and next one with one method expanded.


For the properties that we created with shorthand, C# compiler added a backing field for each shorthand property in this format,
k_BackingField: private TypeOfShrtHandProperty

get_FirstName method expanded
Image shown below looks at the IL generated for get_FirstName method. Second IL instruction 'ldflf' i.e. Load Field, works with backing field created by the compiler. set_FirstName() also uses this backing field.

Don't expect that these shorthands will support validations etc. These are simple properties and just adds thread safety over using class variables directly. If we don't want any thread safely and no validations, then we should not provide properties. I guess this is a cool new feature but unless you understand how it works inside you will not able to fully utilize it.

Next I'll add a small post related to introduction of partial methods in C#. I am down with Viral from last few days but today I am feeling quite well. I hope! to get well soon.

~ILdasm rOcKs~

22 comments:

Anonymous said...

[B]NZBsRus.com[/B]
Escape Slow Downloads With NZB Files You Can Instantly Find High Quality Movies, Console Games, MP3 Albums, Applications and Download Them @ Electric Rates

[URL=http://www.nzbsrus.com][B]Usenet[/B][/URL]

Anonymous said...

[B]NZBsRus.com[/B]
Dismiss Slow Downloads With NZB Files You Can Hastily Search HD Movies, Games, MP3s, Software and Download Them at Flying Speeds

[URL=http://www.nzbsrus.com][B]Newsgroup Search[/B][/URL]

Anonymous said...

Aid to pass the rude with two backs casinos? examine this immature [url=http://www.realcazinoz.com]casino[/url] guide and steppe online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also probe our belated [url=http://freecasinogames2010.webs.com]casino[/url] mastery at http://freecasinogames2010.webs.com and prostrate bona fide tangled bread !
another friendless [url=http://www.ttittancasino.com]casino spiele[/url] purlieus is www.ttittancasino.com , because german gamblers, protect not rococo online casino bonus.

Anonymous said...

Thanks for the details...it was helpful

Anonymous said...

Genial post and this post helped me alot in my college assignement. Say thank you you seeking your information.

Anonymous said...

Someone deleted several links from quickshare and zshare ...

From now, we will use www.tinyurlalternative.com as our main [url=http://www.tinyurlalternative.com]url shortener[/url], so every link will be there and visible for everyone.

You can choose from many great [url=http://kfc.ms]short url[/url] address like:

kfc.ms easysharelink.info jumpme.info megauploadlink.info megavideolink.info mygamelink.info myrapidsharelink.info mytorrentlink.info myurlshortener.com mywarezlink.info urlredirect.info urlshrinker.info weblinkshortener.com youtubelink.info and many others.

They have over 60 other available domains and the [url=http://myurlshortener.com]url shortener[/url] service work properly for free without any registration needed.

So we think it is good notion and propose you to use [url=http://urlredirect.info]url redirect[/url] service too!

Thank you.

Anonymous said...

Good post and this fill someone in on helped me alot in my college assignement. Gratefulness you for your information.

Anonymous said...

Good post and this enter helped me alot in my college assignement. Thank you seeking your information.

Anonymous said...

Sheena is the greatest!?

Daryl

viagra online without prescription said...

Your blog is outrageous! I mean, Ive never been so entertained by anything in my life! Your vids are perfect for this. I mean, how did you manage to find something that matches your style of writing so well? Im really happy I started reading this today. Youve got a follower in me for sure!

Anonymous said...

Message35, http://www.arlo.net/massacree/ viagra online in uk, zjma2, http://www.arlo.net/fccgb/ buy viagra without prescription, jisf3, http://www.arlo.net/fccgb/notes/ buy generic viagra, xjrl9, http://www.arlo.net/bytes/ order cheap viagra, ovzz4, http://www.arlo.net/live/ viagra

Anonymous said...

top [url=http://www.001casino.com/]casino bonus[/url] brake the latest [url=http://www.realcazinoz.com/]casino games[/url] autonomous no deposit bonus at the chief [url=http://www.baywatchcasino.com/]unshackle casino games
[/url].

Anonymous said...

top [url=http://www.001casino.com/]casino online[/url] brake the latest [url=http://www.casinolasvegass.com/]online casino[/url] manumitted no deposit bonus at the leading [url=http://www.baywatchcasino.com/]spare casino games
[/url].

Anonymous said...

john deere 4100 ebook tractor manual http://audiobookscollection.co.uk/de/Structures-and-Beyond-The-Cartography-of-Syntactic-Structures/p224189/ ama manual of style ebook [url=http://audiobookscollection.co.uk/fr/Questions-sociales/c1362/]end zone book ebook[/url] ebook newfangled fariy tales

Anonymous said...

download ebook on wordpress http://audiobookscollection.co.uk/es/Chasing-Trouble/p174303/ linq ebook [url=http://audiobookscollection.co.uk/fr/Globalization-Wages-and-the-Quality-of-Jobs-Five-Country-Studies/p223026/]intro machine learning alpaydin ebook torrent[/url] stokstad art history ebook

Anonymous said...

ebook snapshots from the wedding http://audiobooksworld.co.uk/de/Giant-Axonal-Neuropathy-A-Bibliography-and-Dictionary-for-Physicians-Patients-and-Genome-Researchers/p226490/ turnkey ebook website [url=http://audiobooksworld.co.uk/es/authors/?letter=Zn]wyckoff method ebook[/url] str8te boys ebook torrent

Anonymous said...

Hello. Facebook takes a [url=http://www.casinogames.gd]no deposit casino bonus[/url] stick on 888 casino mete out: Facebook is expanding its efforts to push aside rash real-money gaming to millions of British users after announcing a give out with the online gambling found search for 888 Holdings.And Bye.

Anonymous said...

[url=http://englandpharmacy.co.uk/products/zestril.htm][img]http://onlinemedistore.com/6.jpg[/img][/url]
fuquay varina n c kroger pharmacy http://englandpharmacy.co.uk/products/plendil.htm matt connell pharmacy director [url=http://englandpharmacy.co.uk/categories/cholesterol.htm]cvs pharmacy wakefield ma phone[/url]
mississippi board of pharmacy http://englandpharmacy.co.uk/products/torsemide.htm pharmacy ohio [url=http://englandpharmacy.co.uk/products/kamasutra-longlast-condoms.htm]kamasutra longlast condoms[/url]
how much does a pharmacy tech earn http://englandpharmacy.co.uk/products/erectalis.htm online pharmacy degree [url=http://englandpharmacy.co.uk/products/endep.htm]long term care pharmacy providers[/url]
purchase altace online rolipram pharmacy http://englandpharmacy.co.uk/products/emsam.htm belmont pharmacy school and condo and nashville [url=http://englandpharmacy.co.uk/products/ventolin.htm]ventolin[/url]

Anonymous said...

free dating site romanians [url=http://freeinternetdating.info/friendship/friendship-valentines-gifs-and-cartoons]friendship valentines gifs and cartoons[/url] dating old piano
transexual dating new jersey http://freeinternetdating.info/love/summary-ofto-sir-with-love canadian pinoy dating
talking dating relationship [url=http://freeinternetdating.info/bride/russain-bride-visa-requirement-to-usa]best dating template sites[/url] biltmore pocketwatch dating

Anonymous said...

An outstanding share! I've just forwarded this onto a co-worker who was conducting a little research on this. And he in fact ordered me breakfast because I found it for him... lol. So allow me to reword this.... Thanks for the meal!! But yeah, thanx for spending the time to discuss this matter here on your web page.

Feel free to surf to my weblog - traffic exchange

Anonymous said...

you're really a excellent webmaster. The site loading velocity is amazing. It seems that you are doing any unique trick. In addition, The contents are masterwork. you have done a wonderful job in this matter!

Feel free to surf to my web site: web traffic exchange

Anonymous said...

membership lists for dating sites http://loveepicentre.com/success_stories/ black american dating asian americans
dating prison women [url=http://loveepicentre.com/contact/]ghana dating dites[/url] britney spears dating paparazzi
dating tauranga [url=http://loveepicentre.com/]romanian men dating[/url] free dating site in american [url=http://loveepicentre.com/user/rahulkrishnarbk/]rahulkrishnarbk[/url] bolivian dating sites