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.

Firstly, although MyClass inherits IComparable the compiler doesn't know that it can be converted (sometimes the compiler is dumb). If you change your original definition from MyClass[] mine = new MyClass[5] to IComparible[] mine = new MyClass[5] that will be fine.

Finally, you can only pass an assignable value with the explicit ref keyword. new MyClass(8) isn't assignable, you would need to put this into a variable first and then pass it through.

Strictly speaking, however, you can drop the ref keyword. Your object is being passed by reference anyway.

For Completeness:

When you pass an array into a method, you're passing the pointer to it by value. What this means is that any changes to the contents of the array will be reflected in the original object, however, re-assigning the array will not. That's why you needed the ref keyword. You don't need the ref keyword on the second parameter as it is already passed by reference.

That was very helpful, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.