Skip to content

Instantly share code, notes, and snippets.

@baba-s
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baba-s/9286602 to your computer and use it in GitHub Desktop.
Save baba-s/9286602 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
/// <summary>
/// 列挙型に関する汎用クラス
/// </summary>
public static class EnumCommon
{
private static readonly Random mRandom = new Random(); // 乱数
/// <summary>
/// 指定された列挙型の値をランダムに返します
/// </summary>
public static T Random<T>()
{
return Enum.GetValues(typeof(T))
.Cast<T>()
.OrderBy(c => mRandom.Next())
.FirstOrDefault();
}
/// <summary>
/// 指定された列挙型の値の数返します
/// </summary>
public static int GetLength<T>()
{
return Enum.GetValues(typeof(T)).Length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment