Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


What is the difference between a.Equals(b) and a == b?

Answers were Sorted based on User's Feedback



What is the difference between a.Equals(b) and a == b?..

Answer / bhagyesh

The Equals method is just a virtual one defined in
System.Object, and overridden by whichever classes choose
to do so. The == operator is an operator which can be
overloaded by classes, but which usually has identity
behaviour.

For reference types where == has not been overloaded, it
compares whether two references refer to the same object -
which is exactly what the implementation of Equals does in
System.Object.

Value types do not provide an overload for == by default.
However, most of the value types provided by the framework
provide their own overload. The default implementation of
Equals for a value type is provided by ValueType, and uses
reflection to make the comparison, which makes it
significantly slower than a type-specific implementation
normally would be. This implementation also calls Equals on
pairs of references within the two values being compared.

However, the main difference between the two types of
comparison in normal use (where you're unlikely to be
defining your own value types very often) is polymorphism.
Operators are overloaded, not overridden, which means that
unless the compiler knows to call the more specific
version, it'll just call the identity version. To
illustrate that, here's an example:

using System;

public class Test
{
static void Main()
{
// Create two equal but distinct strings
string a = new string(new char[]
{'h', 'e', 'l', 'l', 'o'});
string b = new string(new char[]
{'h', 'e', 'l', 'l', 'o'});

Console.WriteLine (a==b);
Console.WriteLine (a.Equals(b));

// Now let's see what happens with the same tests
but
// with variables of type object
object c = a;
object d = b;

Console.WriteLine (c==d);
Console.WriteLine (c.Equals(d));
}
}

The results are:

True
True
False
True

The third line is False because the compiler can only call
the non-overloaded version of == as it doesn't know that
the contents of c and d are both string references. As they
are references to different strings, the identity operator
returns false.

So, when should you use which operator? My rule of thumb is
that for almost all reference types, use Equals when you
want to test equality rather than reference identity. The
exception is for strings - comparing strings with == does
make things an awful lot simpler and more readable but you
need to remember that both sides of the operator must be
expressions of type string in order to get the comparison
to work properly.

For value types, I'd normally use == for easier-to-read
code. Things would get tricky if a value type provided an
overload for == which acted differently to Equals, but I'd
consider such a type very badly designed to start with.

Is This Answer Correct ?    39 Yes 3 No

What is the difference between a.Equals(b) and a == b?..

Answer / rajan

equals() method compares the state of an object i.e. it
compares the contents of the two objects.
but == compares the instance of the object i.e. it comparing
the identifier(references).
Ex:
public class Test2
{
public static void main(String args[])
{
// Create two equal but distinct strings
String a = new String(new char[]
{'h', 'e', 'l', 'l', 'o'});
String b = new String(new char[]
{'h', 'e', 'l', 'l', 'o'});

System.out.println(a==b);
System.out.println(a.equals(b));

// Now let's see what happens with the same tests

// with variables of type object
Object c = a;
Object d = b;

System.out.println(c==d);
System.out.println(c.equals(d));
}
}
Output is:
false
true
false
true
From the above example the String object is creating new
instances, so == compares the instances are equal are not.
equal() method compares the contents of a two object .

Is This Answer Correct ?    8 Yes 4 No

What is the difference between a.Equals(b) and a == b?..

Answer / sreejith k

The following article explains in detail on the difference between various equality operators

http://www.dotnetperls.com/string-equals

The final take is that Equals() require that the string be non-null while == checks for null.

Is This Answer Correct ?    1 Yes 2 No

What is the difference between a.Equals(b) and a == b?..

Answer / kavita sharma

no difference

Is This Answer Correct ?    6 Yes 98 No

Post New Answer

More ASP.NET Interview Questions

Why asp.net mvc is better than asp.net? : Asp.Net MVC

0 Answers  


When does the application ONEND event handler fire?

1 Answers   C Squared Systems, Verinon Technology Solutions,


Which ASP.NET configuration options are supported in the ASP.NET implementation on the shared web hosting platform?

0 Answers  


what are the security certificates used in webservices?

0 Answers  


Explain asp.net mvc request life cycle? : asp.net mvc

0 Answers  


Explain the difference between value type and reference type?

0 Answers  


What's the advantage of using System.Text.StringBuilder over System.String?

3 Answers   IBM, IdeaLake, Infosys,


What are the different types of Session state management options available with ASP.NET?

1 Answers  


What is the difference between User Controls and Master Pages

22 Answers   Deloitte, IBM, IntraLogic, Markit, TCS,


What is difference between cache and session?

0 Answers  


What is DTS package?

2 Answers   Accenture, TCS,


List the asp.net validation controls?

0 Answers  


Categories