Running with Code

Like scissors, only more dangerous

  Home  |   Contact  |   Syndication    |   Login
  67 Posts | 0 Stories | 84 Comments | 22 Trackbacks

News



Archives

Post Categories

All Terralever

ASP.NET

Misc

I'm publishing the PowerPoint slides and sample code from the C# 3.0 Best Practices talk I gave tonight at the Arizona .NET Users Group meeting.  Once again I'd like to thank Scott Cate for extending the opportunity to me, Lorin Thwaits for pinch-hitting for Scott, and to Hudson IT Staffing for the event sponsorship.

Please note that the code sample isn't really annotated, and was designed to generate the code in the slides and the Reflector output shown.  The theme of the presentation wasn't so much "How can we do these things the best?" but more "How is this implemented, so I can choose the best way to do something?"  So the code isn't by any means a beacon of effectiveness - it's meant to go with the slides.

I'd welcome feedback and comments, as well as suggestions for additional content.  Enjoy!

posted on Tuesday, February 12, 2008 8:03 PM

Feedback

# re: C# 3.0 Best Practices: Downloads 2/13/2008 9:44 AM Guy
Hey Rob,

Many thanks for taking the time to present this to us yesterday evening.

The tool which incorporates LINQ examples that I was talking about is LINQPad (www.linqpad.net) - kudos to Rob Richardson for introducing me to it.

Guy

# re: C# 3.0 Best Practices: Downloads 2/14/2008 4:39 AM Joe
Rob,
Thanks for posting those slides.

I don't understand this discussion on Object Initializers:

"Always validate input to properties; an exception raised while setting a property will cause a memory leak, even inside try/catch
The object will already be allocated if an exception is raised within a property"

I am not sure how to express what it is that I do not understand, so maybe you can just talk to this a little bit. Typically, I would not validate parameters in an object initializer scenerio (unless they came from an external source, of course, such as user input). For example, if I pull data out of a database (my database) and build an object from it, I would not validate the data first - an exception seems correct in this case (corrupt database data). I also would not fear "memory leaks" because I assume that my "partially-constructed" object would be garbage collected like any properly-constructed object would.

In other words, I image that using object initializers is *equivalent* to (a) creating an object using the default contructor, followed by (b) setting properties. If an exception occurred setting properties - so what? [At least from "memory leak" point of view.]



# re: C# 3.0 Best Practices: Downloads 2/14/2008 1:48 PM Bill Brown
Joe, I was working on an answer to your question when I realized that it doesn't make sense to me either. I'm sure the object initializer is a compiler-trick and that:

Foo bar = new Foo() { Wokka=3, Doodah="stubble" };

is translated to:

Foo bar = new Foo();
bar.Wokka = 3;
bar.Doodah = "stubble";

by the compiler. He seems to be saying that an exception raised during that assignment will cause a memory leak, which I don't get and hasn't been my experience.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 8 and 6 and type the answer here: