- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Enum Equals Method
To find the equality between enums, use the Equals() method.
Let’s say we have the following Enum.
enum Products { HardDrive, PenDrive, Keyboard};
Create two Products objects and assign the same values.
Products prod1 = Products.HardDrive; Products prod2 = Products.HardDrive;
Now check for equality using Equals() method. It would be True since both have the same underlying value.
Example
using System; class Program { enum Products {HardDrive, PenDrive, Keyboard}; enum ProductsNew { Mouse, HeadPhone, Speakers}; static void Main() { Products prod1 = Products.HardDrive; Products prod2 = Products.HardDrive; ProductsNew newProd1 = ProductsNew.HeadPhone; ProductsNew newProd2 = ProductsNew.Speakers; Console.WriteLine("Both are same products = {0}", prod1.Equals(prod2) ? "Yes" : "No"); Console.WriteLine("Both are same products = {0}", newProd1.Equals(newProd2) ? "Yes" : "No"); } }
Output
Both are same products = Yes Both are same products = No
Advertisements