using System.Collections.Generic;
public static void Main()
var mailClass = MailClass.Express;
Console.WriteLine("As string: {0}", mailClass);
Console.WriteLine("Is FirstClass: {0}", mailClass == MailClass.FirstClass);
Console.WriteLine("Is Priority: {0}", mailClass == MailClass.Priority);
Console.WriteLine("Is > FirstClass: {0}", mailClass > MailClass.FirstClass);
Console.WriteLine("Is FedEx: {0}", mailClass == MailClass.FromName("fedex_standard_overnight"));
Console.WriteLine("As integer: {0}", (int)mailClass);
Console.WriteLine("From integer: {0}", (MailClass)300);
private readonly string _name;
private readonly int _value;
public static MailClass FirstClass { get; private set; }
public static MailClass Priority { get; private set; }
public static MailClass Express { get; private set; }
private static List<MailClass> _mailClasses;
private MailClass(string name, int value)
_mailClasses = new List<MailClass>();
FirstClass = new MailClass("FirstClass", 0);
_mailClasses.Add(FirstClass);
Priority = new MailClass("Priority", 1);
_mailClasses.Add(Priority);
Express = new MailClass("Express", 2);
_mailClasses.Add(Express);
_mailClasses.Add(new MailClass("fedex_standard_overnight", 300));
public static MailClass FromName(string name)
return _mailClasses.FirstOrDefault(m => m._name == name);
public static MailClass FromValue(int value)
return _mailClasses.FirstOrDefault(m => m._value == value);
public override string ToString()
public static bool operator == (MailClass x, MailClass y)
return x._value == y._value;
public static bool operator != (MailClass x, MailClass y)
return x._value != y._value;
public static bool operator > (MailClass x, MailClass y)
return x._value > y._value;
public static bool operator < (MailClass x, MailClass y)
return x._value < y._value;
public static bool operator >= (MailClass x, MailClass y)
return x._value >= y._value;
public static bool operator <= (MailClass x, MailClass y)
return x._value <= y._value;
public static explicit operator MailClass(int value)
public static explicit operator int(MailClass mailClass)
public override bool Equals(object obj)
return obj is MailClass other && _value == other._value;
public override int GetHashCode()
return _value.GetHashCode();