I have a problem with code. I receive an error CS0311: The type 'object' cannot be used as type parameter 'T' in the generic type or method 'GenericMethods.search(T[], T)'. There is no implicit reference conversion from 'object' to 'System.IComparable'.
Task: Create a console app and write a generic method, Search [ int Search( T [ ] dataArray, T searchKey) ] that searches an array using linear search algorithm.
a) Method Search should compare the search key with each element in the array until the search key is found or until the end of the array is reached.
b) If the search key is found, return/display its location in the array (i.e. its index value); otherwise return -1.
c) You need to populate the array with random values. ( int values – between 10 and 49, double values between 50 and 99, char values between a and z). The size of array as 10.
d) Test this method by passing two types of arrays to it. (an integer array and a string array) Display the generated values so that the user knows what values he/she can search for.
[Hint: use (T: IComparable) in the where clause for method Search so that you can use method CompareTo() to compare the search key to the elements in the array]
My code:
using System;
using System.Collections.Generic;
namespace LinearSearch { class GenericMethods {
static void Main(string[] args)
{
object[] dataArray = new object[10];
Random random = new Random();
for (int i = 0; i < dataArray.Length; i )
{
int randomType = random.Next(1, 3);
if (randomType == 1)
{
char randomChar = (char)random.Next('a', 'z');
dataArray[i] = randomChar;
}
else if (randomType == 2)
{
double randomDouble = random.Next(50, 99);
dataArray[i] = (int)randomDouble;
}
else
{
int randomInt = random.Next(10, 49);
dataArray[i] = randomInt;
}
}
Console.WriteLine("Generated array is: ");
Console.WriteLine(string.Join(" ", dataArray));
Console.WriteLine("Please, enter number from 10 to 99 or char from A to Z");
object userInput = Console.ReadLine();
Console.WriteLine(userInput);
Console.WriteLine($"Location of search value {userInput}: {search(dataArray, userInput)}");
}//end Main method
private static int search<T>(T[] dataArray, T searchKey) where T : IComparable<T>
{
// default to -1 so that we can determine the search is successful when it becomes a positive value
int arrayPosition = -1;
for (int x = 0; x < dataArray.Length; x )
{
T arrayElement = dataArray[x];
// Console.WriteLine($"CompareTo result: {searchValue.CompareTo(arrayElement)}");
if (searchKey.CompareTo(arrayElement) == 0)
{
// value is found
arrayPosition = x;
break;
}
}
return arrayPosition;
} // end searchValue
}
}
Could you please help me to fix this problem? I am new in C#..
CodePudding user response:
Instead of declaring dataArray
as an object[]
declare it as an IComparable[]
. You wrote the type constraint limiting your generic type T
to IComparable
. object
does not implement IComparable
, so it is not a valid type
CodePudding user response:
I found solution
string[] resultArray = Array.ConvertAll(dataArray, x => x.ToString());
After that code works