Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C# 5.0

Beginning the Rule of Ready Project

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
26 Sep 2013CPOL5 min read 23.3K   207   13  
The beginning of Rule of Ready
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RuleOfReady.Core.Utility
{

    /// <summary>
    /// Extension methods for enums
    /// </summary>
    /// <remarks>
    /// Multiple posters at stackoverflow.com helped with these methods
    /// </remarks>
    public static class EnumExtensions
    {
        /// <summary>
        /// Generic version of GetValues method (why isn't this in the .Net Framework already?)
        /// </summary>
        /// <typeparam name="TEnum">Type of an enum</typeparam>
        /// <param name="someEnum"></param>
        /// <returns></returns>
        /// <remarks>
        /// There is no proper type check for an Enum. This is as close as possible in C# 
        /// </remarks>
        public static IEnumerable<TEnum> GetValues<TEnum>(this TEnum someEnum) 
            where TEnum : struct, IConvertible, IComparable, IFormattable 
        {
            if (!typeof(TEnum).IsEnum)
                throw new ArgumentException(string.Format("{0} must be an enumerated type",typeof(TEnum).FullName));

            return Enum.GetValues(typeof(TEnum)).Cast<TEnum>();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I'm a software developer that works with a small team on websites in .Net. Software development is a career of constant learning, and here I'm learning by sharing.

Comments and Discussions