ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Software  >>  Microsoft Related  >>  ASP.NET
 
 


 

 
 Visual Basic interview questions  Visual Basic Interview Questions
 C Sharp interview questions  C Sharp Interview Questions
 ASP.NET interview questions  ASP.NET Interview Questions
 VB.NET interview questions  VB.NET Interview Questions
 COM+ interview questions  COM+ Interview Questions
 ADO.NET interview questions  ADO.NET Interview Questions
 IIS interview questions  IIS Interview Questions
 MTS interview questions  MTS Interview Questions
 Crystal Reports interview questions  Crystal Reports Interview Questions
 BizTalk interview questions  BizTalk Interview Questions
 Dot Net interview questions  Dot Net Interview Questions
 Exchange Server interview questions  Exchange Server Interview Questions
 SharePoint interview questions  SharePoint Interview Questions
 Microsoft Related AllOther interview questions  Microsoft Related AllOther Interview Questions
Question
What is the difference between a.Equals(b) and a == b?
 Question Submitted By :: .NetGuru
I also faced this Question!!     Rank Answer Posted By  
 
  Re: What is the difference between a.Equals(b) and a == b?
Answer
# 1
no difference
 
Is This Answer Correct ?    3 Yes 23 No
Kavita Sharma
 
  Re: What is the difference between a.Equals(b) and a == b?
Answer
# 2
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 ?    18 Yes 1 No
Bhagyesh
 
 
 

 
 
 
Other ASP.NET Interview Questions
 
  Question Asked @ Answers
 
Does the following statement executes successfully: Response.Write(?value of i = ? + i); TCS4
What are the Application_Start and Session_Start subroutines used for?  5
How do you implement postback with a text box? What is postback and usestate?  1
Explain what a diffgram is, and a good use for one?  1
What property must you set, and what method must you call in your code, in order to bind the data from a data source to the Repeater control?  1
how can you handle "control is not part of this page " error? FactorH3
Is it possible to disable the minimized icon of a popup window using window.open. Not using iframe concept? Take-United2
Security types in ASP/ASP.NET? Different Authentication modes?  3
What does setting a control's EnableViewState property to false accomplish?  1
What are Sticky Sessions? Infosys2
What method must be overridden in a custom control? a) The Paint() method b) The Control_Build() method c) The Render() method d) The default constructor Syntax-Softtech1
Types of caching ? Digital-GlobalSoft1
What is the need of client side and server side validation ? Keane-India-Ltd1
can we call webservice in Html form? Microsoft1
what is view state  4
About DataAdapters ? Accenture7
About Garbage Collector? Microsoft3
what is view state and its use DELL8
We have 2 sites in which one site allows the user with out asking credentials and second one ask for credentials through a log page. What might be the configurations settings for both sites? We can use IIS and web.config files together. ADITI1
what is caching  3
 
For more ASP.NET Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com