Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's difference between Equals and "=="

Status
Not open for further replies.

SonicChen

Programmer
Feb 14, 2004
62
CN
it's really hard to find any book to describe C# fully, basicly and homely here. so i'm a intermediate programmer in C#. i know some class of MS liabrary. i can write some useful code. but i still can't understand some .net framework working such as diference between equals and ==. i feel the requirement for well-knit basic.
thanks firstly to any valuable information about learning C# and this question.
 
For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.

.Equals is a method of the Object object, that is over-ridden by the Equals method of other objects such as;
String.Equals ==> Determines whether two String objects have the same value

So, == is an equality operator, .Equals is a method of an object, comparing the value of two objects of the same type.

This is all in the MSDN Library, but I hope it helps.

Rhys
Buffy: Spike, what are you doing here, five words or less!
Spike: Out for a walk... bitch!
 
= is the assignment operator

== is the comparison operator

Other languages have used different symbols for assignment --- Pascal uses :=.

BASIC (and all it's variants) is the language that most people are familiar with that use the same symbol for both operations (assignment and comparison).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
The comparison operator == and != have different semantics for different predefined types:
•Two expressions of type int are considered equal if they represent the same integer value.
•Two expressions of type object are considered equal if both refer to the same object, or if both are null.
•Two expressions of type string are considered equal if the string instances have identical lengths and identical characters in each character position, or if both are null.
Code:
string s = "George";  		// Here is the assignment operator
string t = string.Copy(s); 	// Here also is the assignment operator
Console.WriteLine(s == t);  	//True
Console.WriteLine((object)s == (object)t); // false!!!
The last output is False because the predefined reference type equality operator is selected when one or both of the operands are of type object.
Reference type equality operators
********************************
The predefined reference type equality operators are:
Code:
bool operator ==(object x, object y);
bool operator !=(object x, object y);
The operators return the result of comparing the two references for equality or non-equality.
Since the predefined reference type equality operators accept operands of type object, they apply to all types that do not declare applicable operator == and operator != members. Conversely, any applicable user-defined equality operators effectively hide the predefined reference type equality operators.
The predefined reference type equality operators require the operands to be reference-type values or the value null. Furthermore, they require that a standard implicit conversion (§6.3.1) exists from the type of either operand to the type of the other operand. Unless both of these conditions are true, a compile-time error occurs.

Delegate equality operators
****************************
Every delegate type implicitly provides the following predefined comparison operators:
Code:
bool operator ==(System.Delegate x, System.Delegate y);
bool operator !=(System.Delegate x, System.Delegate y);
Two delegate instances are considered equal as follows:
•if either of the delegate instances is null, they are equal if and only if both are null.
•If either of the delegate instances has an invocation list (§15.1) containing one entry, they are equal if and only if the other also has an invocation list containing one entry, and either:
• both refer to the same static method, or
• both refer to the same non-static method on the same target object.
• If either of the delegate instances has an invocation list containing two or more entries, those instances are equal if and only if their invocation lists are the same length, and each entry in one’s invocation list is equal to the corresponding entry, in order, in the other’s invocation list.
Note that delegates of different types can be considered equal by the above definition, as long as they have the same return type and parameter types.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top