I want to write a method where i can send any type of [], List<>...etc. But in the code when I use T and I want to index the input array, list or anything the error says "Cannot apply indexing with [] to an expression of type 'T'". Theres any solution for this or I have to write a separate code for array, list..etc?
public void NormSort<T>(ref T input, int n)
{
for (int i = 0; i < n - 2; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (Comparer<T>.Default.Compare(input[i], input[j]) > 0)
{
Swap<T>(input[i], input[j]);
}
}
}
}
public void Swap<T>(T item1, T item2)
{
var temp = item1;
item1 = item2;
item2 = temp;
}
So wheres the input[i], input[j], there i got that error.
Span<T>mechanism; you should be able to create aSpan<T>over most arrays and contiguous lists, and it allows in-placerefaccess to the data. That isn't an option today, though.