若要比较枚举成员,请使用Enum.CompareTo()方法。
首先,为学生设置值。
enum StudentRank { Tom = 3, Henry = 2, Amit = 1 };
现在使用该compareTo()
方法将一个枚举值与另一个枚举值进行比较。
Console.WriteLine( "{0}{1}", student1.CompareTo(student2) > 0 ? "Yes" : "No", Environment.NewLine );
以下是比较C#中的枚举成员的代码。
using System; public class Demo { enum StudentRank { Tom = 3, Henry = 2, Amit = 1 }; public static void Main() { StudentRank student1 = StudentRank.Tom; StudentRank student2 = StudentRank.Henry; StudentRank student3 = StudentRank.Amit; Console.WriteLine("{0} has more rank than {1}?", student1, student2); Console.WriteLine( "{0}{1}", student1.CompareTo(student2) > 0 ? "Yes" : "No", Environment.NewLine ); } }
输出结果
Tom has more rank than Henry? Yes