Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Type Comparison: Type.Equals vs operator ==

ReSharper suggests that the following be changed from:

Type foo = typeof( Foo ); Type bar = typeof( Bar );  if( foo.Equals( bar ) ) { ... } 

To:

if( foo == bar ) { ... } 

operator ==

// Summary: //     Indicates whether two System.Type objects are equal. // // Parameters: //   left: //     The first object to compare. // //   right: //     The second object to compare. // // Returns: //     true if left is equal to right; otherwise, false. public static bool operator ==( Type left, Type right ); 

Equals( Type o )

// Summary: //     Determines if the underlying system type of the current System.Type is the //     same as the underlying system type of the specified System.Type. // // Parameters: //   o: //     The System.Type whose underlying system type is to be compared with the underlying //     system type of the current System.Type. // // Returns: //     true if the underlying system type of o is the same as the underlying system //     type of the current System.Type; otherwise, false. public virtual bool Equals( Type o ); 

Question
Why would operator == be recommended over Equals( Type o ) when comparing Types?

like image 211
Metro Smurf Avatar asked Feb 10 '12 19:02

Metro Smurf


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

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?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language 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 ...


2 Answers

I suggest that you read the excellent When is a Type not a Type? blog post by Brad Wilson. To summarize: a runtime type (represented by the internal type RuntimeType), managed by the CLR, is not always the same as a Type, which can be extended. Equals will check the underlying system type, whereas == will check the type itself.

A simple example:

Type type = new TypeDelegator(typeof(int)); Console.WriteLine(type.Equals(typeof(int))); // Prints True Console.WriteLine(type == typeof(int));      // Prints False 
like image 168
Julien Lebosquain Avatar answered Oct 12 '22 11:10

Julien Lebosquain


The reason is simple: The two are functionally equivalent in this case and the latter is more readable.

like image 27
Justin Niessner Avatar answered Oct 12 '22 11:10

Justin Niessner