If you ever dare Googeling “Java vs C#” you will undoubtly be witness to the religion wars that exist between the opposing factions of syntax warriors. I’ve always just seen both languages as great tools, but it was not until recently I saw just how similar they are.

I was polishing my knowledge of searching and sorting algorithms, and I was doing a binary search in C#.

Later I was writing the same code in Python and then in Java. After having confirmed my solutions I thought my Java solution was quite similar to my C# solution:

## C#

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
static int binarySearch(int[] arr, int searchValue) { int low = 0; int high = arr.Length - 1; while (low<=high) { int mid = (low+high)/2; if (arr[mid] < searchValue) low = mid + 1; else if (arr[mid] > searchValue) high = mid - 1; else return mid; } return -1; } |

## Java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
static int binarySearch(int[] arr, int searchValue) { int low = 0; int high = arr.length - 1; while (low<=high) { int mid = (low+high)/2; if (arr[mid] < searchValue) low = mid + 1; else if (arr[mid] > searchValue) high = mid - 1; else return mid; } return -1; } |

## Python

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def binarySearch(arr, searchValue): low = 0 high = len(arr) - 1 while (low<=high): mid=int((low+high)/2) if (arr[mid] < searchValue): low = mid + 1 elif (arr[mid] > searchValue): high = mid - 1 else: return mid return -1 |

## Javascript

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function binarySearch(arr, searchValue) { var low = 0; var high = arr.length - 1; while (low<=high) { var mid = parseInt((high+low)/2); if (arr[mid] < searchValue) low = mid + 1; else if (arr[mid] > searchValue) high = mid - 1; else return mid; } return -1; } |

## PHP

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function binarySearch($arr, $searchValue) { $low = 0; $high = count($arr) - 1; while ($low<=$high) { $mid = intval(($low+$high)/2); if ($arr[$mid] < $searchValue) $low = $mid + 1; elseif ($arr[$mid] > $searchValue) $high = $mid - 1; else return $mid; } return -1; } |

It turns out there is **one thing** that seperates them. C# uses arr.Length (note Capital L in Length) where Java just uses length. I also did one in Python, Javascript and PHP – they’re all quite similar too

texttexttext`code`

more code

~~~~