using System;
namespace ConsoleApplication1
{
class TypedEnum<T>
{
public T Value { get; private set; }
public TypedEnum(T value)
{
this.Value = value;
}
public override string ToString()
{
return this.Value.ToString();
}
public override int GetHashCode()
{
return 0;
}
public override bool Equals(object obj)
{
if (obj is TypedEnum<T>)
{
return this.Value.Equals(((TypedEnum<T>)obj).Value);
}
else
{
return false;
}
}
public static bool operator ==(TypedEnum<T> a, TypedEnum<T> b)
{
if (Object.Equals(a, null))
{
return Object.Equals(b, null);
}
return a.Equals(b);
}
public static bool operator !=(TypedEnum<T> a, TypedEnum<T> b)
{
if (Object.Equals(a, null))
{
return !Object.Equals(b, null);
}
return !a.Equals(b);
}
}
// ISO 5218を元に作成
class 性別 : TypedEnum<string>
{
private 性別(string value) : base(value) { }
public static readonly 性別 男 = new 性別("1");
public static readonly 性別 女 = new 性別("2");
}
class Program
{
static void Main(string[] args)
{
性別 sex;
sex = 性別.男;
Console.WriteLine("男 : {0}", sex);
sex = 性別.女;
Console.WriteLine("女 : {0}", sex);
String value = sex.Value;
Console.WriteLine("女 : {0}", value);
Console.WriteLine("true : {0}", sex == 性別.女);
Console.WriteLine("false : {0}", sex == 性別.男);
Console.WriteLine("true : {0}", 性別.男 == 性別.男);
Console.WriteLine("true : {0}", 性別.女 == 性別.女);
Console.WriteLine("false : {0}", 性別.男 == 性別.女);
Console.WriteLine("false : {0}", 性別.女 == 性別.男);
Console.WriteLine("false : {0}", 性別.男 == null);
Console.WriteLine("false : {0}", sex == null);
Console.WriteLine("false : {0}", null == 性別.男);
Console.WriteLine("false : {0}", null == sex);
sex = null;
Console.WriteLine("true : {0}", sex == null);
Console.WriteLine("true : {0}", null == sex);
}
}
}