You really should know and use these... seriously...
Back to blog listSimple things apparently annoy me a great deal...
.NET Developers should be aware of, and use, .NET constructs that have been around for ages:
- String.Empty - avoid creation of empty string references ("") use this built in single reference and the String.IsNullOrEmpty() method too.. the framework is there to help you!
- String.Format - avoid concatenation using + or &. Use string.format with the objects you intend to concatenate, it will also do the ToString for you and has formatting abilities.
- StringBuilder - strings are immutable. String builders avoid the creation of new strings each time you do a & or + concatenation.
- Using - Wrapping IDisposable instances in Using statements will automatically call dispose immediately on End Using. Additionally, implement IDisposable on heavy classes so that your own code can be consumed this way.
- Try Catch Finally - No catch should be empty, either decorate the exception with an appropriate wrapper before throwing again or remedy whatever the problem was because just plain eating exceptions makes debugging particularly crappy. Also use Finally to ensure that even when exceptions are thrown references are disposed of appropriately.
- 'As' (TryCast in VB) - Should be used where appropriate to cast and return null if the cast fails (rather than throw a cast exception).
- TryParse - Should be used instead of Try Catch blocks i.e. Integer.TryParse will return false if the parse failed, rather than throwing an exception.
- Nullable Types should be used for primitive types that can also be null i.e. int? or Nullable(Of Integer). This struct has a 'hasValue' property that can be accessed rather than building your own constructs such as -1 means no value, or 0 means no value etc.
- Generics - Do not use classes such as ArrayList or SortedList as they are object based and have the overhead of casting to and from object as well as forcing the user to do these casts. Compile time checking is also hindered. Instead use Generic Collections so that typing can be enforced i.e. Generic.SortedList<int, MyClass> or Generic.SortedList(Of Integer, MyClass). Additionally look to use generics to create generalised code (avoid similar operations being redone in multiple specialised classes). Generics is not just applicable to classes. You can, and should if necessary, create Generic Methods e.g. ConfigurationSettings.GetValue<int>("keyName") allows casting and other checking to be encapsulated away from consuming code.
Obviously it's up to you in the context of each situation you encounter, but to never use these at all is surely madness.
Published: April 2009Other Recent Posts
dConstruct 2010(September 2010)
It seems that I mainly Blog (as opposed to write 'articles') after attending some kind of nerd based event, but here I am again, just after an event typing away...
Microsoft UK Tech Days 2010(April 2010)
Continuing the theme of keeping myself busy with geek events! I also recently attended the Microsoft UK Tech Days Visual Studio and .NET 4 talks.
Barcamp Bournemouth 2(April 2010)
Having been to a few of the 'big' conferences and just returned from SXSW it was quite refreshing to attend a more small scale, local affair. With other barcamp's often being either far away or both far away and starting too soon after work BarCamp Bournemouth made it easy, running over a weekend and being so close!
SXSW - In Reflection(March 2010)
Having seen (or at least made notes on) 17 talks at last year's 'South By', coupled with the fact that this year pretty much the entire Headscape team were heading out to Austin I felt sure that my 'talk total' would be much lower. However I somehow managed to make it out to 21 different talks, panels and/or podcast recordings!
SXSW - Fifth Day round up(March 2009)

