Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# enum equals() vs ==

Tags:

c#

enums

equals

In the case of using enums, is it better to use:

if (enumInstance.Equals(MyEnum.SomeValue)) 

or to use

if (enumInstance == MyEnum.SomeValue) 

Are their any important considerations using one vs the other?

like image 839
ToddBFisher Avatar asked Jul 05 '13 15:07

ToddBFisher


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


1 Answers

If the compile-time type of enumInstance is the enum type, you're fine with ==.

If the compile-time type of enumInstance is Enum, ValueType or Object, you need to use Equals. (You'll get a compile-time error if you try to use == in that case.)

Note that your enum currently violates .NET naming conventions - it would normally be MyEnum.Value.

like image 53
Jon Skeet Avatar answered Sep 27 '22 22:09

Jon Skeet