Created
September 9, 2010 05:50
-
-
Save cz75hiro/571448 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
[STAThread] | |
static void Main(string[] args) | |
{ | |
TestFlag f = TestFlag.b | TestFlag.d; //bとdにフラグを立てる | |
Console.WriteLine("{0}", f.HasFlag(TestFlag.a)); | |
Console.WriteLine("{0}", f.HasFlag(TestFlag.b)); | |
Console.WriteLine("{0}", f.HasFlag(TestFlag.c)); | |
Console.WriteLine("{0}", f.HasFlag(TestFlag.d)); | |
Console.WriteLine("{0}", f.HasFlag(TestFlag.e)); | |
Console.WriteLine("{0}", f.HasFlag(TestFlag.f)); | |
Console.Read(); | |
} | |
} | |
[Flags] | |
public enum TestFlag | |
{ | |
//値が被らないように設定 | |
a = 0x0001, | |
b = 0x0002, | |
c = 0x0004, | |
d = 0x0008, | |
e = 0x0010, | |
f = 0x10000000 | |
} | |
/// <summary> | |
/// Enum拡張メソッド | |
/// </summary> | |
public static class EnumExtension | |
{ | |
/// <summary> | |
/// フラグを持っているか | |
/// </summary> | |
/// <param name="f"></param> | |
/// <param name="e"></param> | |
/// <returns></returns> | |
public static bool HasFlag(this Enum f, Enum e) | |
{ | |
/* fとeの論理積を取って、eと比較 | |
* f=00101 | |
* e=00100の時 | |
* 00100を返す | |
*/ | |
return (f & e) == e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment