overloading operator==


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 ?
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

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().
 
Ad

Advertisements


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