overload for "operator == "

J

Jongmin Lee

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
 
M

Mattias Sjögren

Jongmin,
//can not use if ( b == null), because endless looping

if ( (object)b == null )

or

if ( Object.ReferenceEquals( b, null ) )

should work



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top