Spontaneous Publicity
blogs are the new phone book

C# Language Improvements: Know What You Are Getting Into

February 6, 2008 03:21 by Luke

The C# language has changed a lot since its initial release in 2000. C# 2.0 brought us:

imageC# 3.0 brought us:

And that is just the new language features in the last 3 years! This does not include all of the new classes added to the BCL over the same time period.

Know the Cost

I am as excited about these new changes as the next guy. I have used various combinations of these improvements in my projects with great success. But is there a cost to new features?

Recently I read a couple of articles that reminded me that you not only have to understand how a feature is used, you also have to understand the cost of using it. Sometimes that cost is performance, other times it could be something like readability or portability. I'll take one example from the 2.0 framework and then one from the 3.5 framework to show that you need to understand the implications of using new features.

Yield Return

First, there is Fritz Onion's recent post on the amount of code generated by the yield return syntax introduced in C# 2.0:

The array allocation function generated a total of 20 lines of IL, but the yield return function, if you included all of the IL instructions for the IEnumerable class generation as well was over 100! That's a 5x penalty in code generation to save 18 characters of typing

So is the 5x penalty ever worth it? Yes, of course it is. There are things you can do with yield that you couldn't do previously. In most cases, the use of yield improves readability dramatically. It also makes the task of creating a custom enumerator much easier.

In his article The power of yield return, Joshua Flanagan points to an example where using the yield return improved performance dramatically:

With the improved implementation that took advantage of the yield keyword, the program was able to finish its job in less than half the time! It also used much less memory, as it never had to store all 9 strings in a collection. Now imagine the potential impact if GetCombinations returned a collection with thousands of entries!

The point here is that you have to know the costs and how they fit in with the requirements of the project you are working on. If you are building something that must support thousands of concurrent users and must be very performant, you will most likely choose to use the yield return despite the extra code generated by the compiler.

LINQ is Dead, Long Live LINQ

Most of the time when I show somebody the new language enhancements in C# 3.0, the thing that interests them the most (and rightly so) is LINQ. It is just so different to see query syntax built right into the language. The next question they usually ask is how the performance of something written using LINQ would compare the same procedure written using just C# 2.0 control structures.

Steve Hebert recently wrote a post about LINQ performance where he concludes:

Despite my best efforts I just couldn’t make my hand-written code perform as poorly as Linq

What? Seriously? Wow. Then I guess we should stay away from LINQ then right? Well, if you look closer at his post, you will notice that he has optimized his non LINQ version of the code a little bit for the underlying data structure where LINQ has to be able to operate regardless of the underlying data structure. I am not so much interested in the particulars so much as I am the idea that you should understand these things before you start replacing all of your for loops with LINQ queries.

Shifting Sands

One more recent post that fits in here. Rob Conery wrote about his recent experience where his 5 year old code has come back to haunt him:

I spose the moral of the story is to always view the concept of maintenance with an eye towards shifting toolsets and platforms. In 4 years you will need to support the ASP.NET 2.0 site you’re on now, using Visual Studio 2012 and it’s Silverlight-generated Scaffolds :).

This is a great reminder of how tough it can be to deal with changes in technology. I mean, come on, he couldn't find a machine to compile his 5 year old code?!? I know we can all relate. With the technology learning curve growing at a semi exponential way, just imagine where that same code will be in another 5 years.

Back to Machine Code

Obviously I am not advocating that we just give up on new features because they might not be as fast or resource efficient as our hand coded machine code. In fact, I think we need to embrace abstractions wherever possible. They make our jobs as software developers much easier. Just be smart about it. Have some idea what is going on behind the covers. It is important to realize that yield return eventually becomes a bunch of compiler generated code and that LINQ queries are general case and they may not always be as great as manually doing the coding yourself.

Maybe the documentation on these new language features could do a better job at explaining what sorts of considerations we need to take into account when using them in our applications.

kick it on DotNetKicks.com
Tags:
Categories:
Actions: E-mail | del.icio.us | Permalink | Comments (114) | Comment RSSRSS comment feed

Comments

March 6. 2008 07:06

Joshu


          I am OCD about following LINQ. One point I  think your post misses that deserves equal time is what LINQ will allow in future iterations of its code base. With PLINQ being talked about openely. I don't think it is out of the question for MS to slip stream perf improvements into then old LINQ code. With doing everything manually you side step this benifit and will have to opt in. There is only so much guessing MS will do to speed up a for or while statement.

          Just like SQL and other four generation languages improve with age. LINQ being the same concept. It will improve without coder intervention as time goes on. I think in 2012 code will be a lot more disposable than it is now when projects start embracing modularity instead of lets redo x, y, z that _____ product/project already does.
        

Joshu

July 10. 2010 15:06

Financial Banking

I would like to thank you for the efforts you have made in writing this <A href="http://taxbanking.com">financial banking</A> . I am hoping the same best work from you in the future as well.

Financial Banking

July 11. 2010 02:23

Marc Halliwell

thank you for this valuable information I will post a link on my site so my readers can benefit from it as well.

Marc Halliwell

July 11. 2010 14:35

Islam Blogger

When I read this post it reminds me of my old <A href="http://islamblogger.com">Islam Blogger</A>! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!

Islam Blogger

July 11. 2010 19:26

Islam Blogger

I thought it was going to be some boring old post, but it really compensated for my <A href="http://islamblogger.com">Islam Blogger</A> . I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Islam Blogger

July 12. 2010 02:46

Free Payroll Software

Thanks for the wonderful ideas.  Please meditate upon some of the many blessings in this world, and share them with others.  It is a stupendous honor to spread around good things.  Work hard to do something special for other people however you are able.  Make everything more special for everyone.

Free Payroll Software

July 12. 2010 19:40

Angland14270@hotmail.com

Very worthwhile page.  Your own site is fairly quickly becoming certainly one of my top picks.

Angland14270@hotmail.com

July 12. 2010 23:52

Leodora

Many thanks for the benefit!

Leodora

July 14. 2010 08:23

eve online isk

This is a great web site.  Good polished UI and nice informative articles. I will be coming back in a bit, thanks for the great article.

eve online isk

July 14. 2010 19:28

EDI

Really Good and well written article ! David.

EDI

July 14. 2010 22:20

Cursos de ingles en el extranjero

I like that you've touched on this subject, it's somewhat of a rariety.

Cursos de ingles en el extranjero

July 15. 2010 10:06

Fatburning furnace scam

Hey there - I should say, I sure am amazed with your site. I had no problems navigating through all of the tabs and  the info was very easy to access. I discovered what I wanted in no time at all. Truly awesome.

Fatburning furnace scam

July 15. 2010 12:28

Cursos de idiomas en el extranjero

I like that you've touched on this subject, it's somewhat of a rariety.

Cursos de idiomas en el extranjero

July 15. 2010 15:50

Accounting Software for Small Business

Awww...  Thanks for sharing.

Accounting Software for Small Business

July 15. 2010 16:06

Accounting Software for Small Business

Interesting, but not ideal.  When will we know more?  

Accounting Software for Small Business

July 16. 2010 20:58

residential standby generator

everyone needs to get involved

residential standby generator

July 18. 2010 01:45

Accounting Software Download

Interesting, but not my cup of tea.  When will we know more?  

Accounting Software Download

July 19. 2010 02:59

autoblog blueprint 2

I think that is an interesting point, it made me think a bit. Thanks for sparking my thinking cap.

autoblog blueprint 2

July 19. 2010 09:23

eve online isk

This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!

eve online isk

July 20. 2010 08:34

webcamsex

I love this topic. We look forward to new comments on this topic to see your opinions.

webcamsex

July 20. 2010 20:32

Nike Shox NZ

Your post really helped. It seems very interesting. Hope to read more about your Fashion tips. keep it up!

Nike Shox NZ

July 20. 2010 23:59

Cheap Web Hosting

This has come in handy for me, thanks.

Cheap Web Hosting

July 21. 2010 00:04

Andrew Pelt

I think I will have to blog about this as well...

Andrew Pelt

July 21. 2010 01:08

Cheap Web Hosting

Thanks for this article!

Cheap Web Hosting

July 21. 2010 04:45

uggs outlet

The post of content is very interesting and exciting. I learned a lot from here.The content from simple to complex, so all of you can come in . No matter you want to see what can be found.By the way ,there are some websites is also very wonderful,you can go and see.such as  <a href="http://www.discount-air-jordan.com " >jordan shoes</a>

uggs outlet

July 23. 2010 04:01

Issac Maez

Its good to be back in your blog. I found lot of something new again and I learn a lot again. Let me give credits in your blog... Thank you so musch and post more contents again.....

Issac Maez

July 23. 2010 10:09

illinois for sale by owner

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

illinois for sale by owner

July 23. 2010 17:40

Kim Kardashian tape

this story looks familiar...

Kim Kardashian tape

July 23. 2010 21:59

Improve Spelling

Thought-provoking, this is exactly ideal.  Will there be more?  

Improve Spelling

July 24. 2010 00:07

Andrew A. Sailer

what plugins are you using on your blog?

Andrew A. Sailer

July 25. 2010 04:32

Matthew C. Kriner

why are you not adding more revenue sources to your blog?

Matthew C. Kriner

July 25. 2010 16:12

aluminum signs

Child custody  fights hairstyles  http://carlos676wilkins.hpage.com    are frequently complicated . hairstyles http://www.frackdp.com As a  family law advocate , I frequently see other  lawyers  hairstyles http://www.frackdesign.com  milking  concerned parents tens of thousands of dollars which destroys marital assets. My minimum retainer for a  custody case  is hairstyles http://joico-hairstyle-tips.wikispaces.com  $30,000  because hairstyles http://www.smartmarklabels.com  I know the other party is going to drag it out as long as he or she can .  Invariably , hairstyles http://carlos676wilkins752.webs.com one  side hairstyles http://www.zimbio.com/joico-hairstyle-advice  will  accuse my client of  allegations of  domestic violence, sexual harassment, or other typically nonsense rumors  in order to hairstyles http://carlos676wilkins.bravejournal.com  win .   I hairstyles http://www.labels-by-wfco.com have been  recommending that  my clients try mediationhairstyles http://carlos676wilkins.wiki.zoho.com    which can save them thousands of dollars .  Many local judges have been hairstyles http://www.williamfrick.com  agreeing with  my efforts as well.  If  there is   any reader who  would like to  learn  more about other, like-minded, localhairstyles http://joico-hair-advice.ning.com , please see this website.

aluminum signs

July 25. 2010 22:27

china dropship

Surprisingly positive job, this article in fact should get only compliment critics. Especially I loved the last step. Yes, the increased inches around your waist may well actually get a concern.

china dropship

July 25. 2010 23:06

breitling emergency

Thank you for sharing

breitling emergency

July 26. 2010 06:29

Michelle Kinlecheeny

I must admit that this is one great insight. It surely gives a company the opportunity to get in on the ground floor and really take part in creating something special and tailored to their needs.

Michelle Kinlecheeny

July 26. 2010 11:04

consignment

Good luck getting people behind this one.  Though you make some VERY fascinating points, youre going to have to do more than bring up a few things that may be different than what weve already heard.  What are trying to say here?  What do you want us to think?  It seems like you cant really get behind a unique thought.  Anyway, thats just my opinion.

consignment

July 26. 2010 11:59

Burton Haynes

how old is this blog?

Burton Haynes

July 27. 2010 03:54

Burton Haynes

I want to know your secret to have so many comments...

Burton Haynes

July 27. 2010 07:18

cleveland multiple listing

This topic has been up for debate quite a lot of times but none of the posts were as detailed as yours. AdminI hope to see such quality posts from you in the future.

cleveland multiple listing

July 27. 2010 19:44

Refurbished Computers

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon... Regards…  http://www.pctechoutlet.com

Refurbished Computers

July 28. 2010 07:41

eve isk

This is a great web site.  Good polished UI and nice informative articles. I will be coming back in a bit, thanks for the great article.

eve isk

July 28. 2010 18:53

winston cheap mls

I recently came across your web site and have been reading along. I thought I would leave my very first comment. Nice blog. I will keep visiting this website very frequently.

winston cheap mls

July 28. 2010 21:07

natural cures for bacterial vaginosis

Impressive article, loads of helpful knowledge.

natural cures for bacterial vaginosis

July 29. 2010 15:18

Issac Maez

I have read about this on newspaper a few week ago...

Issac Maez

July 31. 2010 02:01

buy eve isk

This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!

buy eve isk

August 1. 2010 00:09

Luigi Fulk

how old is this blog?

Luigi Fulk

August 1. 2010 12:16

ImHosted Review

Thank you for this article, It's great to see another BlogEngine.NET user. Most users these days tend to use other systems like Wordpress, but I think BlogEngine.NET is the best system to use.

ImHosted Review

August 1. 2010 14:44

Pregnancy Miracle Review

Greetings there! Very useful post! I'm very happy that I was able to stumble upon your blog while searching Google. Thank you for this great post!

Pregnancy Miracle Review

August 3. 2010 03:53

xloop wholesale

Thank you for sharing

xloop wholesale

August 3. 2010 23:54

tattoos books

you can write a book about this...

tattoos books

August 4. 2010 11:09

polymyositis treatment

Substantially, the post is really the sweetest on this valuable topic. I concur with your conclusions and will thirstily look forward to your future updates. Just saying thanks will not just be enough, for the wonderful clarity in your writing. I will instantly grab your rss feed to stay privy of any updates. Pleasant work and much success in your business efforts!

polymyositis treatment

August 4. 2010 11:38

abercrombie

Very often I go to see this internet site. It very greatly is pleasant to me. Thanks the author

abercrombie

August 5. 2010 01:42

tattoo books

you can write a book about this...

tattoo books

August 6. 2010 03:02

tattoos book

when do you plan to change the theme of your blog? This one is really old...

tattoos book

August 6. 2010 14:05

sex web cams

Yes, it seems interesting topic. What do you think guys? <a href="http://www.naughtywebcamsex.com" title="Webcam Girls">Webcam Girls</a>

sex web cams

August 6. 2010 18:17

live webcam girls

Good topic. Your blog provided me with valuable information to help me get started my own blog in this branch. I will continue to watch. Thanks!<a href="http://www.naughtywebcamsex.com" title="Webcam Girls">Webcam Girls</a>

live webcam girls

August 6. 2010 21:40

eve isk

You had fantastic good ideas here. I did a search on the subject and discovered almost all peoples will agree with your blog.

eve isk

August 7. 2010 22:56

meetgirls

As an amateur writer from Romania, I rarely come across really quality publications like this blog which really helps me inspirationally. It shows me what excellent writings are all about. There are lots of low quality writings on the web and it's very sad. Thank you for your well written posts! I frequently read your blog and enjoy the content you share. I look forward to more of your work.

meetgirls

August 9. 2010 03:18

Jenine Aderson

what about adding some pics too...

Jenine Aderson

August 9. 2010 14:34

metal detect books

anyone interested in metal detecting?

metal detect books

August 10. 2010 14:07

Swimming Tips

What a interesting information.  Please meditate upon the myriad good things in your life, and share them with others.  It is a great honor to pass along joy.  Work hard to give something nice for other people however you can.  Make life better for everyone.

Swimming Tips

August 10. 2010 16:08

Nell Prenatt

Good article, numerous quality knowledge.  I'm going to point out to my friends and ask them what they think.

Nell Prenatt

August 11. 2010 10:34

Designer Consignment

Congratulations on having one of the most sophisticated blogs Ive come across in some time!  Its just incredible how much you can take away from something simply because of how visually beautiful it is.  Youve put together a great blog space --great graphics, videos, layout.  This is definitely a must-see blog!

Designer Consignment

August 11. 2010 22:30

african american funeral poems

I've read over numerous of your entries and I had been curious if you wanted to swap blog site links?  I have a website on the subject of funeral poetry, and I am usually seeking to trade links with blogs on comparable readers!  I look forward to hearing back from you quickly.

african american funeral poems

August 12. 2010 01:54

kim kardashian make up

want to watch a great tape?

kim kardashian make up

August 13. 2010 08:48

Claretha Kinsman

TGIF, have a great week end!

Claretha Kinsman

August 14. 2010 08:32

Richelle Sharbono

proffesional service is a must

Richelle Sharbono

August 15. 2010 07:25

acne medicine

what about giving more details?

acne medicine

August 15. 2010 14:39

Demarcus Kmet

You made some good points there. I did a search on the topic and found most people will agree with your blog.

Demarcus Kmet

August 16. 2010 03:02

eve online isk

This is a great web site.  Good polished UI and nice informative articles. I will be coming back in a bit, thanks for the great article.

eve online isk

August 16. 2010 18:13

Accounting Software Download

Buck up!  Tomorrow is another day.

Accounting Software Download

August 17. 2010 01:29

Madie Erskin

any plan to post more frequently?

Madie Erskin

August 17. 2010 22:52

usb data transfer cable

I was wondering what is up with that weird gravatar??? I know 5am is early and I'm not looking my best at that hour, but I hope I don't look like this! I might however make that face if I'm asked to do 100 pushups. lol

usb data transfer cable

August 17. 2010 23:15

Betsey Brechtel

busy blog...

Betsey Brechtel

August 19. 2010 02:05

Brittny Pirre

a contact form will be useful...

Brittny Pirre

August 19. 2010 22:29

Broyhill Furniture

Cheers for this post,  it was great to read.

Broyhill Furniture

August 20. 2010 01:34

Brain Exercises

Hola.  I really like your blog.  Mind if I link to it?

Brain Exercises

August 20. 2010 05:04

acne scars

how can I advertise on this site?

acne scars

August 21. 2010 01:50

Web Based Accounting Software

Cheer up!  Tomorrow is another day.

Web Based Accounting Software

August 21. 2010 02:49

Josiah Dwelle

are you selling ads on your blog?

Josiah Dwelle

August 21. 2010 15:18

Fat Loss for Idiots Review

I think you have spent a lot of time to provide this amazing post to us. I don't know how to thank you.

Fat Loss for Idiots Review

August 22. 2010 03:01

dumb blonde jokes

anyone know some good jokes?

dumb blonde jokes

August 22. 2010 16:33

Lucien Weakland

Great story! I have been looking for this information all around. This is was I looking for.

Lucien Weakland

August 22. 2010 18:19

Online Consignment

Intimately, the post is in reality the greatest on this valuable topic. I agree with your conclusions and will thirstily look forward to your incoming updates. Just saying thanks will not just be enough, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates. Admirable work and much success in your business enterprise!

Online Consignment

August 23. 2010 01:38

blonde joke of the day

want a good blonde joke?

blonde joke of the day

August 23. 2010 13:12

Online Consignment

I would like to thank you for the efforts you have made in composing this article. I am going for the same best work from you in the future as well. In fact your fanciful writing abilities has prompted me to start my own blog now. Actually the blogging is spreading its wings rapidly. Your write up is a fine example of it.

Online Consignment

August 24. 2010 06:06

personal training business plan

anyone want to be a personal trainer?

personal training business plan

August 25. 2010 03:59

gift guides

any idea what is the best gift for women?

gift guides

August 26. 2010 06:11

seduction books

anyone know a good seduction book?

seduction books

August 27. 2010 12:27

botkier

As an amateur writer from London, England, I infrequently come across really quality publications like this blog which really helps me inspirationally. It shows me what superb articles are all about. There are tons of substandard writings on the web and it's quite sad. Thank you for your well written posts! I frequently read your blog and enjoy the content you share. I look forward to more of your work.

botkier

August 27. 2010 18:56

Dennise Igo

do you have an rss feed? I want to add it to my reader but I can't find it...

Dennise Igo

August 28. 2010 22:21

Alvaro Rothbauer

are you using wp for this blog?

Alvaro Rothbauer

August 29. 2010 20:58

new driver sign

I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.

new driver sign

August 30. 2010 15:14

blonde joke

nice blog! keep the good work!

blonde joke

August 30. 2010 16:02

dating affiliate

you may make some money if you add an affiliate program to your blog...

dating affiliate

August 30. 2010 18:42

bail bonds baltimore

I was wondering if you ever considered changing the layout of your blog?  Its very well written; I love what youve got to say.  But maybe you could a little more in the way of content so people could connect with it better.  Youve got an awful lot of text for only having one or two images.  Maybe you could space it out better?

bail bonds baltimore

August 31. 2010 00:06

payroll company

Congratulations on having one of the most sophisticated blogs Ive come across in some time!  Its just incredible how much you can take away from something simply because of how visually beautiful it is.  Youve put together a great blog space --great graphics, videos, layout.  This is definitely a must-see blog!

payroll company

August 31. 2010 14:38

wine bottle

Hi admin ! iam comin again with new blog ?

wine bottle

August 31. 2010 20:32

refurbished garmin gps

I think the information written in your post is really excellent. I've been working on a preliminary research project on this topic and your article really helped with a number of considerations that I had. I am brainstorming a research paper for college and I?m currently following many blogs for review.

refurbished garmin gps

September 1. 2010 01:18

learn to type

Nice job! Looking for a typing tutor online? Check out my website.

learn to type

September 1. 2010 03:03

Baby Gifts, New Baby Gifts

This is a good posting, I was wondering if I could use this write-up on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away

Baby Gifts, New Baby Gifts

September 1. 2010 10:02

gym training

don't spend all the day in front of your computer, go out, exercise, go to the gym twice a week...

gym training

September 1. 2010 20:42

typing tutor

I enjoyed reading your post! Learn to type faster with typing tutor online.

typing tutor

September 2. 2010 03:44

fitness training

don't forgot to get out and exercise...

fitness training

September 2. 2010 12:45

red ring of death

How could i record excellent Xbox 360 elite in good shape? I just ordered 2 weeks in and also Means genuinely wish to hold proper care from it. Accordingly have the ability to individual give me top tips to use offers System in good shape?

red ring of death

September 3. 2010 06:19

ETFs

any plan to update this blog?

ETFs

September 3. 2010 21:32

mixed wrestling

I am speechless. This is a gripping blog and very engaging too. Nice work! That's not really much coming from an amateur writer like me, but it's all I could say after diving into your posts. Great grammar and vocabulary. Unlike other blogs. You really know what you're talking about too. So much that you made me want to explore more. Your blog has become a stepping stone for me, my fellow blogger. Thank you for the detailed journey. I really enjoyed the 4 posts that I have read so far.

mixed wrestling

September 4. 2010 04:03

dating

do you have a xml feed for your blog?

dating

September 5. 2010 04:39

ETFs

do you have a different blog where you post more often?

ETFs

September 6. 2010 07:08

ETF

just bookmarked your blog...

ETF

September 7. 2010 04:27

Carpet Cleaning Philadelphia

A round of applause for your article.Much thanks again. Great.

Carpet Cleaning Philadelphia

September 7. 2010 11:45

Theta Meditation

An additional I have worked with is utilizing the breathing in mixture with soothing tunes. I figure what creates a piece of tunes soothing will vary for everyone, but Native American drumming and flute functions nicely for me, as well as Enya and certain classical tunes pieces.

Theta Meditation

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading