April 27, 2010
Say you have a class with a property that’s a string. Using object initializers you can do this little nugget:
Product p = new Product { ProductName = Console.ReadLine() };
It will work as you’d expect (prompt you for input, then assign it to ProductName. However, that’s pretty easy to miss when you’re quickly scanning code. I’m generally in favor of fewer lines of code, but in this case it would be good design to just break this into two lines.
Written by Scott Williams who lives and works in sunny Phoenix, AZ. Twitter is also a place.