Apology in advance if this is an obvious question.
I just switched from java to C# and I am doing practice in C# in order to get myself familiar with it. It's just a binary search program.
- there is an error in this line
key > array[mid]cannot apply indexing with [] to an expression of type system.array. - I tried to google it and someone said it should be
key > array.GetValue(mid),but after changing it the error becomes "Operator < cannot be applied to operands of type int and object".
I really don't know what to do and I don't have anybody to discuss it, so any help or any advice about switching from java to C# would be great. Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BinarySearch.BinarySearch
{
class BinarySearch
{
public int BinarySearch(int key, Array array)
{
int lowerbound = 0, upperbound = array.Length, mid = 0, count = 0, midValue = 0;
while (lowerbound <= upperbound)
{
mid = (lowerbound + upperbound)>>1;
if (key < array[mid])
{
upperbound = mid - 1;
count++;
}
else if (key > array[mid])
{
lowerbound = mid + 1;
count++;
}
else
{
count++;
return mid;
}
}
return -1; //the key does not exist in this array.
}
}
}
Array arrayshould beint[] array