Click here to Skip to main content
15,562,903 members
Articles / Programming Languages / C# 3.5
Technical Blog
Posted 20 Aug 2015


3 bookmarked

Writing Simple Odd Even Kata in C#

Rate me:
Please Sign up or sign in to vote.
4.22/5 (4 votes)
20 Aug 2015CPOL2 min read
Writing simple odd even Kata in C#

Few days ago, I delivered a talk on ‘Test Driven Development’. More detail: Test Driven Development.

Just after session, I’ve received many queries and lot of responses from a great audience, here is one of the good questions, asked during session by a great guy Balwinder Singh.

Q. How Do I Find Odd Even in C# Using Test Driven Development Approach?

The answer is very simple, we just need to follow the same approach as detailed below.

Please note that I am not going to copy/paste the entire code – the reason being I do not want to steal your time. :) I have attached the complete code as well, you can get the same one from Github:
Odd even katas by Gaurav Kumar Arora

Let's start to get the walk through – how we can write this tdd kata?

public static string PrintOddEven(int startNumber, int lastNumber)  
return GetOddEvenWithinRange(startNumber, lastNumber);  

In the above snippet, we have a simple method which is accepting two parameters, startNumber and lastNumber, it's obvious that we are defining the limit of our numeric series to find-out the odd-even.

public static string PrintSingleOddEven(int number)  
return CheckSingleNumberOddEvenPrimeResult(number);  

Great, we also have one other method, which tells us whether the entered number is odd or even. :)

Now, let's test our first method to find out odds and evens from within the provided range of integers:

[Category("OddEven Kata")]  
public class TestOddEven  
[TestCase(1, 50)]  
[TestCase(1, 100)]  
[TestCase(5, 150)]  
public void CanPrintOddEven(int startRange, int endRange)  
var result = OddEven.PrintOddEven(startRange, endRange);  
Assert.NotNull(result, string.Format("{0}", result));  

In the above, we are using NUnit framework to create tests, I have another post describing all about NUnit framework (if you want to read all about Nunit).

So, in the above, we have three test cases with different series of integers, it will provide all numbers quoting Odd or Even.

You just need to run the code and you will get the desired results. :)

The post Writing simple Odd even Kata in C# appeared first on


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
India India
There is no age limit for learning and its never late, to start.

Comments and Discussions

QuestionWhat's the purpose of the test CanPrintOddEven? Pin
John Brett21-Aug-15 7:14
John Brett21-Aug-15 7:14 
Looking in GitHub, I can see a test CanPrintOddEvenForSingleNumber, which makes a lot of sense to me.

However, CanPrintOddEven just tests that the result is not null.
I could implement the code to pass the test with
return string.Empty;
which I don't think meets the intent.

AnswerRe: What's the purpose of the test CanPrintOddEven? Pin
Gaurav Aroraa23-Aug-15 22:55
professionalGaurav Aroraa23-Aug-15 22:55 
QuestionWhat is the pluspoint of Kata TDD compared to MS TESTS ? Pin
fredatcodeproject21-Aug-15 6:39
professionalfredatcodeproject21-Aug-15 6:39 
AnswerRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
Gaurav Aroraa23-Aug-15 22:47
professionalGaurav Aroraa23-Aug-15 22:47 
GeneralRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
fredatcodeproject24-Aug-15 0:41
professionalfredatcodeproject24-Aug-15 0:41 
AnswerRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
Gaurav Aroraa24-Aug-15 0:46
professionalGaurav Aroraa24-Aug-15 0:46 
GeneralRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
fredatcodeproject28-Aug-15 2:14
professionalfredatcodeproject28-Aug-15 2:14 
AnswerRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
Gaurav Aroraa29-Aug-15 12:01
professionalGaurav Aroraa29-Aug-15 12:01 
QuestionQuestion/observation about method calls Pin
Slacker00721-Aug-15 4:51
professionalSlacker00721-Aug-15 4:51 
AnswerRe: Question/observation about method calls Pin
Gaurav Aroraa21-Aug-15 5:15
professionalGaurav Aroraa21-Aug-15 5:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.