
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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
- Related Articles
- AngularJS – equals() method
- The equals and == operator for Enum data type in Java
- Java String equals() method.
- C# Enum IsDefined Method
- C# Enum ToString() Method
- C# Enum Parse Method
- C# Enum TryParse() Method
- C# Enum CompareTo Method
- C# Enum Format Method
- C# Enum GetName Method
- C# Enum GetNames Method
- C# Enum GetValues Method
- Integer Equals() method in Java
- LocalTime equals() method in Java
- MonthDay equals() method in Java

Advertisements