ASP.NET Weblogs

Welcome to ASP.NET Weblogs Sign in | Join | Help
in Search

The Technical Adventures of Adam Weigert

C#: My First Extension Method

I will find many, many uses for this ... maybe someone else will too!

using System;
using System.Diagnostics;

internal static class StringExtensions
{
    public static T ToEnum<T>(this string value)
        where T : struct
    {
        Debug.Assert(!string.IsNullOrEmpty(value));
        return (T)Enum.Parse(typeof(T), value, true);
    }
}

Published Dec 08 2007, 10:01 AM by adweigert
Filed under: , ,

Comments

 

Fons Sonnemans said:

Hi Adam,

I also like extension methods. That why I have created the ExtensionMethod.net website to publish them freely. I have added your ToEnum() method in it, I hope you don't mind.

www.extensionmethod.net/Details.aspx

Regards,

Fons

December 9, 2007 3:55 PM