I know that there is already a way to find if a number is even or odd using the modulus (What is the fastest way to find if a number is even or odd?). However, I was wondering if there is a C# function like Math.Even or Math.Odd. Is the only way to do this by using a modulus?
-
1You probably shouldn't bet if you haven't even looked it up yet. I'll assume you mean whether or not such a method exists within the .NET framework (it's not a language feature): no, not as far as I know. – Jeroen Vannevel Dec 15 '14 at 16:41
-
Actually I did do some research, I even provided a link of one of the sites I visited. – Ian Wise Dec 15 '14 at 16:41
-
1There isn't one that I know of, sorry. – JayDev Dec 15 '14 at 16:42
-
Just to clarify, is the requirement that the C# language include the functionality or that some heap of software compatible with C#, e.g. .NET Framework, might offer some assistance? – HABO Dec 15 '14 at 16:51
4 Answers
It may count as cheating, but if you use BigInteger, it has an IsEven method.
As stated in MSDN, calling this method is equivalent to:
value % 2 == 0;
Reference:

- 30,738
- 21
- 105
- 131

- 25,801
- 18
- 85
- 151
-
6
-
2@LucasTrzesniewski: If anything, they both can have a good laugh and remain friends. :) – Victor Zakharov Dec 15 '14 at 16:47
Actually, there are more interesting points, and some other methods to check is number even. When you use %
, you should check your values with 0 as was mentioned by others, because comparing with 1 will give the wrong answer with all negative integers.
bool is_odd(int n) {
return n % 2 == 1; // This method is incorrect for negative numbers
}
bool is_odd(int n) {
return n % 2 != 0;
}
The second popular way is demonstrated below.
bool is_odd(int n) {
return x & 1 != 0;
}
This method makes use of the fact that the low bit will always be set on an odd number.
Many people tend to think that checking the first bit of the number is faster, but that is not true for C# (at least). The speed is almost the same and often modulus works even faster.
There is the article where the author tried out all popular ways to check if the number is even and I recommend you to look at the tables that are demonstrated at the bottom of the article.

- 30,738
- 21
- 105
- 131

- 941
- 1
- 14
- 37
-
What do you mean by *"...to check is number even"* (seems incomprehensible)? Do you mean *"...to check if a number is even"* (one deletion and three insertions)? (The last paragraph contains *"...check if the number is even"*.) Or something else? – Peter Mortensen Jun 24 '22 at 11:19
-
There is no method in .NET that just calls %2==0
for you. Such a simple method presumably isn't worth their time to implement for you, given that the alternative is literally five characters.
You can of course write your own named method to perform this calculation if you really want to.

- 202,030
- 26
- 332
- 449
-
@IanWise The increment operator saves quite a bit more than just two characters. In many circumstances it can be a pretty dramatic difference. The fact that the reference to be incremented could be a complex expression, with side effects, but that it is only executed once, can take some doing to replicate. – Servy Dec 15 '14 at 16:41
-
@IanWise Incrementation can also be used in many many more places than "check for eve/odd" - dive deep enough into iteration, particularly with longer variable names, and you'll quickly see this happening. – David Dec 15 '14 at 16:43
-
1You'd need `%2==0`. It's 5 characters, not 2. This is 2.5 times more complicated ;) – Lucas Trzesniewski Dec 15 '14 at 16:45
Using (myVal % 2) == 0
is good, but bitwise operators are fast too:
(myVal & 0x1) == 0
Works for positive and negative values, and different types (e.g. 16-bit or 64-bit).

- 5,413
- 6
- 45
- 99