Click here to Skip to main content
15,890,579 members
Home / Discussions / C#
   

C#

 
Questionhow to open Image n Video file header in c# Pin
shrikant121329-Oct-09 8:29
shrikant121329-Oct-09 8:29 
AnswerRe: how to open Image n Video file header in c# Pin
Dave Kreskowiak29-Oct-09 10:39
mveDave Kreskowiak29-Oct-09 10:39 
AnswerRe: how to open Image n Video file header in c# Pin
Abhishek Sur29-Oct-09 10:40
professionalAbhishek Sur29-Oct-09 10:40 
GeneralRe: how to open Image n Video file header in c# Pin
Christian Graus29-Oct-09 12:06
protectorChristian Graus29-Oct-09 12:06 
GeneralRe: how to open Image n Video file header in c# Pin
shrikant121329-Oct-09 15:33
shrikant121329-Oct-09 15:33 
GeneralRe: how to open Image n Video file header in c# Pin
Christian Graus29-Oct-09 18:47
protectorChristian Graus29-Oct-09 18:47 
GeneralRe: how to open Image n Video file header in c# Pin
shrikant121330-Oct-09 6:15
shrikant121330-Oct-09 6:15 
QuestionObject.Equals(object one, object two) vs Object.ReferenceEquals(object one, object two) [SOLVED] Pin
CaptainSeeSharp29-Oct-09 8:16
CaptainSeeSharp29-Oct-09 8:16 
They both appear to do the same thing, except Object.Equals documentation says that it throws a NullReferenceException. I can't get it to do that though.

They both test for equality of instances, they both take the same parameters, and they are both static members of System.Object.

I am implementing equality comparison using the standard overrides and operator overloads. I am frustrated with these little details that seem unnecessary.

Documentation says:


Object.ReferenceEquals Method
Determines whether the specified Object instances are the same instance.


Object.Equals Method
Determines whether two Object instances are equal.


EDIT:

I figured it out, I stepped through the code and learned that the static Object.Equals(object a, object b) called in the operator overload actually makes a call to the virtual instance-level Equals(object obj), so the static ReferenceEquals has to be used to test for instance equality. Though by default the static Object.Equals(object a, object b) will do the same thing as the static ReferenceEquals, unless you override the instance-level Equals method, because by default, the instance-level equals tests for instance equality.

public class Foo : IEquatable<Foo>
{
    public static bool operator !=(Foo foo1, Foo foo2)
    {
        return !Equals(foo1, foo2); //under the hood, this STATIC, note STATIC method makes a call to the
                                    // INSTANCE-level Equals(object obj) method.
    }

    public static bool operator ==(Foo foo1, Foo foo2)
    {
        return Equals(foo1, foo2); //under the hood, this STATIC, note STATIC method makes a call to the
                                    // INSTANCE-level Equals(object obj) method.
    }

    public bool Equals(Foo foo)
    {
        if (foo == null) return false;
        return y == foo.y && x == foo.x;
    }

    public override bool Equals(object obj) //called by the static object.Equals(obj, obj) method
    {
        if (ReferenceEquals(this, obj)) return true;  
                                                     
        return Equals(obj as Foo);
    }

    public override int GetHashCode()
    {
        return y + 29*x;
    }

    private int y;
    private int x;
}



modified on Thursday, October 29, 2009 2:48 PM

AnswerRe: Object.Equals(object one, object two) vs Object.ReferenceEquals(object one, object two) Pin
Ian Shlasko29-Oct-09 8:34
Ian Shlasko29-Oct-09 8:34 
GeneralRe: Object.Equals(object one, object two) vs Object.ReferenceEquals(object one, object two) Pin
Henry Minute29-Oct-09 8:36
Henry Minute29-Oct-09 8:36 
GeneralRe: Object.Equals(object one, object two) vs Object.ReferenceEquals(object one, object two) Pin
CaptainSeeSharp29-Oct-09 8:47
CaptainSeeSharp29-Oct-09 8:47 
AnswerRe: Object.Equals(object one, object two) vs Object.ReferenceEquals(object one, object two) Pin
Henry Minute29-Oct-09 8:34
Henry Minute29-Oct-09 8:34 
GeneralRe: Object.Equals(object one, object two) vs Object.ReferenceEquals(object one, object two) [modified] Pin
CaptainSeeSharp29-Oct-09 8:40
CaptainSeeSharp29-Oct-09 8:40 
Questionmake video Pin
behzadcp29-Oct-09 7:38
professionalbehzadcp29-Oct-09 7:38 
AnswerRe: make video Pin
Henry Minute29-Oct-09 8:39
Henry Minute29-Oct-09 8:39 
AnswerRe: make video Pin
EliottA29-Oct-09 8:40
EliottA29-Oct-09 8:40 
GeneralRe: make video Pin
_Madmatt29-Oct-09 9:46
_Madmatt29-Oct-09 9:46 
GeneralRe: make video Pin
EliottA29-Oct-09 9:49
EliottA29-Oct-09 9:49 
GeneralRe: make video Pin
_Madmatt29-Oct-09 9:56
_Madmatt29-Oct-09 9:56 
GeneralRe: make video Pin
EliottA29-Oct-09 9:58
EliottA29-Oct-09 9:58 
GeneralRe: make video Pin
_Madmatt29-Oct-09 10:02
_Madmatt29-Oct-09 10:02 
GeneralRe: make video Pin
EliottA29-Oct-09 10:03
EliottA29-Oct-09 10:03 
GeneralRe: make video Pin
_Madmatt29-Oct-09 10:05
_Madmatt29-Oct-09 10:05 
GeneralRe: make video Pin
EliottA29-Oct-09 10:06
EliottA29-Oct-09 10:06 
GeneralRe: make video Pin
_Madmatt29-Oct-09 10:08
_Madmatt29-Oct-09 10:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.