overloading operator==

  • Thread starter Thread starter volkan
  • Start date Start date
V

volkan

Hi,
I have a student class. In this class i overloaded ==,!= and equals so that
they will compare the studentids ( which is a member of the class ).
However i have a problem with null check.
public static bool operator !=(Student s1, Student s2)
{
...
how can i check if s1 or s2 is null or not in this overload.
if i do s1==null it goes recursive
if i typecast s1 down to object class, it seems not working
any ideas ?
 
volkan said:
I have a student class. In this class i overloaded ==,!= and equals so that
they will compare the studentids ( which is a member of the class ).
However i have a problem with null check.
public static bool operator !=(Student s1, Student s2)
{
...
how can i check if s1 or s2 is null or not in this overload.
if i do s1==null it goes recursive
if i typecast s1 down to object class, it seems not working
any ideas ?

Casting s1 up to object should be fine. What problems are you having?
An alternative is to use object.ReferenceEquals().
 
Back
Top