Snippet: Ruby-like Hashes in C#

The ASP.NET MVC team developed a URL routing engine, similar to the engine in Rails, which has made it’s way into the rest of ASP.NET. ScottGu has covered it a few times in the past, and this post has some good examples of it. Here’s a quick example:

routes.MapRoute(”ShowProductByCategory”, “Products/Show/{Category}/{id}”,
new { controller […]

Snippet: Strongly typed property names

Last night at the QMSDNUG someone asked whether it was possible to implement INotifyPropertyChanged without hard-coding property names as strings inside code. For example, you would normally write:
public string FirstName
{
get { return _firstName; }
set
{
_firstName = […]

Tip: Locking when calling external code

Can you spot the danger in the following code?
public T GetItem(Func<T, bool> finder)
{
T result = default(T);
lock (_itemsLock)
{
foreach (T item in _items)
{
[…]

Snippet: Multi-Valued Enums - Extension Method Style

Omar blogged a method to extract the selected values in an enumeration:
http://blog.omarbesiso.net/index.php/2008/02/13/c-multi-valued-enumerators-flags/
Suppose you had an flags enumeration called Option. You could use his method to write the following code:
Options options = Options.ReadOnly | Options.Archive;
Console.WriteLine("You selected:");
foreach (Options option in EnumHelper.GetSelectedEnumValues<Options>(options))
{
Console.WriteLine(option);
}
While a generic static method is fine, I think this is a good […]

Snippet: String.Format Extension Method

Early this year Mitch suggested a string formatting syntax for C# vNext. His suggestion was that string.Format could be replaced with syntax like this:
throw new Exception( @("{0} {1} {2}"|a|b|c) );
I think it’s a great suggestion and I too would like to see it.
In the mean time, I’ve been making use of the following extension […]