So, I wrote some code for appending to an array, and I thought to myself, would be cool to make the method more generic using an interface. I keep getting this error though that basically says that the compiler's best overloaded method that can match this is _____, and it does not like how I am passing it. So I am getting the distinct impression that interfaces cannot be passed by reference. Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test2 {
class Program {
static void Main(string[] args) {
MyClass[] mine = new MyClass[5];
for (int i = 0; i < mine.Length; i++)
mine[i] = new MyClass(i);
foreach (MyClass temp in mine)
Console.WriteLine(temp.ToString());
appendIComparable(ref mine, ref new MyClass(8) );
foreach(MyClass temp in mine)
Console.WriteLine(temp.ToString());
pause();
}
public static void pause() {
Console.Write("Press any key to continue... ");
Console.ReadLine();
}
public class MyClass : IComparable {
public int i;
public MyClass(int i) { this.i = i; }
public int CompareTo(object obj) {
if (obj == null) return 1;
MyClass otherClass = obj as MyClass;
if (otherClass != null)
return this.i.CompareTo(otherClass.i);
else
throw new ArgumentException("Object is not my class... ");
}
public override string ToString() {
return i.ToString();
}
}
public static void appendIComparable(ref IComparable[] original, ref IComparable append) {
//move original from one array to the other
int j = 0;
IComparable[] newArray = new IComparable[original.Length + 1];
for (; j < original.Length; j++) {
if (original[j].CompareTo(append) > 0) {//if the append customer is larger than the current customer in the first list
j++;
break;
}
else
newArray[j] = original[j];
}
newArray[j] = append;//then add the append customer to the list
for (; j < original.Length; j++)//and then finish up appending the customers from the first list to the end list
newArray[j] = original[j - 1];
append = null;
original = newArray;
}//end method
}//end class
}//end namespace
So am I wrong, or is this just not possible. Also, was for a customer comparison method before, so comments don't quite match up.