When you index an element of List<T>
you are actually accessing the this
indexer, which is a kind of property (i.e. has getter and setter methods). You can only pass variables as ref
or out
, not properties.
In your scenario, perhaps you want something more like this:
public class ConvexHull
{
List<Point> points;
public void run ()
{
swap(0, 1); //No error!!
}
private void swap(int i, int j)
{
Point point = points[i];
points[i] = points[j];
points[j] = point;
}
}
A more general solution might look like this:
public class ConvexHull
{
List<Point> points;
public void run ()
{
points.SwapElements(0, 1);
}
}
static class Extensions
{
public static void SwapElements<T>(this List<T> list, int index1, int index2)
{
T t = list[index1];
list[index1] = list[index2];
list[index2] = t;
}
}
In either case, the correct approach is to provide the code that is actually swapping values with access to the List<T>
object itself, so that it can access the indexer property to accomplish the swap.