overload for "operator == "

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jongmin Lee

    #1

    overload for "operator == "

    Hi everybody,

    I overloaded "operattor= = " for comparing two objects.
    However, I can't compare one instance to null valued
    object instance.
    How to solve this problem?

    public class Custom
    {
    private int m_idx = 0;
    public int Idx
    { get.... set ......}
    ..
    ..
    public static bool operator ==(Custom a, Custom b)
    {
    //can not use if ( b == null), because endless looping

    return a.Idx == b.Idx;
    }
    ..
    ..
    ..
    }


    ..
    ..
    ..
    Custom a = new Custom();
    Custom b = null;
    if (b == null)
    // b = a; to do something


  • Mattias Sjögren

    #2
    Re: overload for "operat or == "

    Jongmin,
    [color=blue]
    >//can not use if ( b == null), because endless looping[/color]

    if ( (object)b == null )

    or

    if ( Object.Referenc eEquals( b, null ) )

    should work



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    Working...