Let's make a list of answers where you post your excellent and favorite extension methods.
The requirement is that the full code must be posted and a example and an explanation on how to use it.
Based on the high interest in this topic I have setup an Open Source Project called extensionoverflow on Codeplex.
Please mark your answers with an acceptance to put the code in the Codeplex project.
Please post the full sourcecode and not a link.
Codeplex News:
24.08.2010 The Codeplex page is now here: http://extensionoverflow.codeplex.com/
11.11.2008 XmlSerialize / XmlDeserialize is now Implemented and Unit Tested.
11.11.2008 There is still room for more developers. ;-) Join NOW!
11.11.2008 Third contributer joined ExtensionOverflow, welcome to BKristensen
11.11.2008 FormatWith is now Implemented and Unit Tested.
09.11.2008 Second contributer joined ExtensionOverflow. welcome to chakrit.
09.11.2008 We need more developers. ;-)
09.11.2008 ThrowIfArgumentIsNull in now Implemented and Unit Tested on Codeplex.
For Winform Controls:
IsDesignTime Usage:
SetDropdownWidth Usage:
I forgot to mention, feel free to use these on Codeplex...
Are these any use?
I miss the Visual Basic's With statement when moving to C#, so here it goes:
And here's how to use it in C#:
Saves a lot of typing!
Compare this to:
put in codeplex project
Below is an extension method that adapts Rick Strahl's code (and the comments too) to stop you having to guess or read the byte order mark of a byte array or text file each time you convert it to a string.
The snippet allows you to simply do:
If you find any bugs please add to the comments. Feel free to include it in the Codeplex project.
The extension method:
The method applies for all types and lets you add a range of items to a list as parameters.
Example:
By all means put this in the codeplex project.
Serializing / Deserializing objects to XML: