KJ Price
KJ Price

Reputation: 5984

Proper way to compare integers

I'm not an expert in C# but know that I should not simply use == when comparing strings. Is there a similar .Equals method that I should use when comparing ints?

For example, I would like to do something like:

if (someInt == 2) {}

Is that acceptable in C#?

Upvotes: 5

Views: 31643

Answers (5)

Ahmad Pujianto
Ahmad Pujianto

Reputation: 359

Personnally I like better using .Equal() Because it will check both value and data type.

So int 200 != string "200" != double 200 .. etc

And it will not throwing exception like using == over 2 different type of data.

example : int x = 200; string y = "200";

if(x==y){ } //==> this will throw exception and block execution

Upvotes: 0

David
David

Reputation: 236

I think I can clear up a little confusion here. The choice of using .Equals over == in String is actually from Java. In Java, String is wrapped and treated like an object, which can cause some issues, including null pointer exceptions. This doesn't arise in c# however, because String is a basic type, and String.Equals(a, b) is defined as a == b. String.Equals does give you some options by allowing you to add a comparison type. This is a slight advantage to String.Equals, but in no way should you always use one over the other. However, I felt it was necessary to note that String.Equals should be used in Java as the two languages are very similar and could be confused by a beginner. That being said, the above answers regarding int a == int b and Int32.Equals are all valid, I would use == more often for ints because it is a more commonly accepted practice.

Upvotes: 2

matrixanomaly
matrixanomaly

Reputation: 6967

As int is a value type, it cannot be null.

Hence you can use == just fine.

However, if you have an MyInteger class (a wrapper class for value type int, that inherits from an Object type) that can be a null object, in which it does not contain an int within it. See this MSDN regarding Boxing and Unboxing, where an int is boxed and assigned to an object.

Back to the question, you can use == just fine for int types, here are a few other alternatives:

a. CompareTo method

e.g: 5.CompareTo(6) //returns -1 This will return -1 if first int is smaller, 0 is they are equal, and 1 if first int is larger. This method is similar to < > == operators.

b. Int32.Equals method This is identical to == as it returns a true/false boolean. See an example from MSDN here. However there is a difference of this method compared to == for a boxed int, as Jon Skeet detailed in this SO question, this is related to the boxing and unboxing I mentioned

Upvotes: 6

JB Reefer
JB Reefer

Reputation: 93

You can absolutely use == for Strings in C#, we have operator overrides.

Upvotes: 0

Servy
Servy

Reputation: 203812

I should not simply use == when comparing strings

Why not? (Assuming another culture or some other non-default comparison mechanism is appropriate for you.) It's a perfectly sensible operator for strings, just like it is for ints. It tells you whether the two values are equal, just as one would expect.

It's not the only way to compare two string or ints for equality, but it's certainly valid one.

Upvotes: 10

Related Questions