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