• 14
• 12
• 9
• 10
• 13

# [.net] Simple Comparisons Question

This topic is 4964 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Just found something that seems a little odd (at least to me).
if (student["StudentID"].Equals(uint.Parse(txtStudentID.Text)))
...


That works properly, but I can't use:
if (student["StudentID"] == uint.Parse(txtStudentID.Text))
...


It gives me an Operator '==' cannot be applied to operands of type 'object' and 'uint' error. Can someone explain why that would be the case? Oh, I should probably mention, student["StudentID"] is a uint (System.UInt32). Even double-checked that with GetType(). Thanks in advance, -hellz

##### Share on other sites
Though it may be referencing a uint, student["StudentID"] is of type object. The .NET framework does not define an == operator for the object class, but it does define an Equals method. So when you do Equals it resolves the uint.Equals member in the virtual table and compares the two uints.

If you want to use ==, you have to explicitly cast student["StudentID"] to a uint.