Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Comparing enum members in C#
To compare enum members in C#, you can use the Enum.CompareTo() method or standard comparison operators. Enum comparison is based on the underlying numeric values assigned to each enum member.
Syntax
Following is the syntax for using CompareTo() method −
enumValue1.CompareTo(enumValue2)
Following is the syntax for using comparison operators −
enumValue1 == enumValue2 // equality enumValue1 > enumValue2 // greater than enumValue1 < enumValue2 // less than
Return Value
The CompareTo() method returns −
-
Negative value if the first enum is less than the second
-
Zero if both enums are equal
-
Positive value if the first enum is greater than the second
Using CompareTo() Method
Example
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}", student1.CompareTo(student2) > 0 ? "Yes" : "No");
Console.WriteLine("\nCompareTo() return values:");
Console.WriteLine("Tom.CompareTo(Henry): " + student1.CompareTo(student2));
Console.WriteLine("Henry.CompareTo(Amit): " + student2.CompareTo(student3));
Console.WriteLine("Amit.CompareTo(Tom): " + student3.CompareTo(student1));
}
}
The output of the above code is −
Tom has more rank than Henry? Yes CompareTo() return values: Tom.CompareTo(Henry): 1 Henry.CompareTo(Amit): 1 Amit.CompareTo(Tom): -2
Using Comparison Operators
Example
using System;
public class Demo {
enum Priority { Low = 1, Medium = 5, High = 10 };
public static void Main() {
Priority task1 = Priority.High;
Priority task2 = Priority.Medium;
Priority task3 = Priority.Low;
Console.WriteLine("Comparison using operators:");
Console.WriteLine("High > Medium: " + (task1 > task2));
Console.WriteLine("Medium == Low: " + (task2 == task3));
Console.WriteLine("Low < High: " + (task3 < task1));
Console.WriteLine("\nSorting by priority:");
Priority[] tasks = {Priority.Low, Priority.High, Priority.Medium};
Array.Sort(tasks);
foreach(Priority p in tasks) {
Console.WriteLine(p + " (value: " + (int)p + ")");
}
}
}
The output of the above code is −
Comparison using operators: High > Medium: True Medium == Low: False Low < High: True Sorting by priority: Low (value: 1) Medium (value: 5) High (value: 10)
Comparing Enum Values in Conditional Statements
Example
using System;
public class Demo {
enum Grade { F = 0, D = 1, C = 2, B = 3, A = 4 };
public static void Main() {
Grade student1 = Grade.B;
Grade student2 = Grade.C;
Grade passingGrade = Grade.D;
Console.WriteLine("Student grades comparison:");
if (student1.CompareTo(student2) > 0) {
Console.WriteLine("Student1 (Grade {0}) performed better than Student2 (Grade {1})", student1, student2);
}
if (student1 >= passingGrade) {
Console.WriteLine("Student1 passed with grade: " + student1);
}
if (student2 >= passingGrade) {
Console.WriteLine("Student2 passed with grade: " + student2);
}
}
}
The output of the above code is −
Student grades comparison: Student1 (Grade B) performed better than Student2 (Grade C) Student1 passed with grade: B Student2 passed with grade: C
Conclusion
Enum comparison in C# is based on the underlying numeric values of enum members. You can use CompareTo() method for detailed comparison results or standard operators (==, >, <) for direct boolean comparisons. This makes enums useful for representing ordered data like priorities, grades, or rankings.
