tags:

views:

146

answers:

3

I have an assignment in a language-independent class, and part of it is using templated functions like


    T functionName(T &param1[], T &param2[]){
        // do stuff
    }
    

I would like to write this program in C#, but I've ran into a problem.

How can I make this work in C#:

<pre><code>
T functionName(ref List<T> param1, ref List<U> param2){
    // do stuff
}
</code></pre>

?

+3  A: 

If you intended param2 to be List<U>

T functionName<T,U>(ref List<T> param1, ref List<U> param2)

Otherwise:

T functionName<T>(ref List<T> param1, ref List<T> param2)
siz
+1  A: 

Try something like this (note the <T,U> after the function name):

T functionName<T,U>(ref List<T> param1, ref List<U> param2){
    // do stuff
}
Andrew Hare
+3  A: 

You've got two answers that cover generics... just an aside: it is unusual is C# to need ref, since you are already passing just the reference (List<T> is a reference-type object). The ref is needed only if you are assigning new lists inside the method and want the client to see the re-assignment. Changes to the list(s) will already be seen without the ref.

Marc Gravell
Yup, I was going to post this if you hadn't.
Ray Hidayat