Is it possible to declare a collection of many generic delegates with different types?
<p>I have a question about C# generics and delegate: </p>

<p>First I describe the general question---I want a collection of delegates, these delegates should have a similar form, for example, all my delegates should have the form:take two parameters of the same type, and return int. I guess the best way to model these delegates is by using generic delegate:</p>

<pre><code>public delegate int MyFunctionDel &lt;T&gt; (T a, T b);
</code></pre>

<p>But how can I create a collection of MyFunctionDel with different type? I cannot declare this:</p>

<pre><code>List&lt;MyFunctionDel &lt;T&gt;&gt; mylist; //Compile error: cannot find type T
</code></pre>

<p>And secondly, here is what I am actually trying to do. What I am trying to do could be solved by the above question. But you can give alternative solutions.</p>

<p>I wrote a collection-like structure: it can hold any type of data. But all the data in the structure should be of the same type. Unfortunately, this structure is not generic for some historical reason. This structure has a Compare method. </p>

<p>But now I need to provide customized comparers for some specific type. And the behavior I want is: the struture uses customized comparer if there is one for the type of data in it, otherwise uses the original Compare method. Here is my demo code:</p>

<pre><code>/*
 *This piece of code demonstates my idea, but never works
 */

static class Program
{
    [STAThread]
    static void Main()
    {
        MyStructure s = new MyStructure ();

        //create a customized comparer using Comparison&lt;T&gt; generic delegate
        Comparison &lt;string&gt; myStirngComparer = (x , y)=&gt; {
           return -x.CompareTo(y);
        };

        s.CustomizedComparers[typeof(string)] = myStirngComparer;

        System.Console.WriteLine (s.Compare("a" , "b")); //I am expecting the result to be 1
    }
}

class MyStructure
{
   //For simplicity, I won't put storage related code here

    public int Compare (object o1, object o2)
    {
       //let's suppose o1 and o2 are always of same type
       switch (o1.GetType())
       {
            case TypeCode.Single: return Compare&lt;Single&gt; ((Single)o1 , (Single)o2);  
            case TypeCode.Double: return Compare&lt;Double&gt; ((Double)o1 , (Double)o2); 
            case TypeCode.String: return Compare&lt;String&gt; ((String)o1 , (String)o2); 
            //and so on.....
       }
       return 0;            
    }

    //NOTE: code below won't work
    //But my logic is: use a Dictionary to store a map from "Type" to "Comparison&lt;T&gt;"
    //When doing the comparison, we first examine if there exists one Comparison&lt;T&gt; for 
    //Type T in the Dictionary

    //Compile failed here
    public Dictionary &lt;Type , Comparison&lt;T&gt; &gt; CustomizedComparers = new Dictionary &lt;Type , Comparison&lt;T&gt; &gt; ();

    private int Compare&lt;T&gt; (T a , T b)
    {
         if (CustomizedComparers.ContainsKey(typeof(T)))
         {
            Comparison&lt;T&gt; comp = CustomizedComparers[typeof(T)];
            return comp (a , b);
         }
         else return Comparer&lt;T&gt;.Default.Compare(a, b);
    }
}
</code></pre>

<p>Any comments, suggestions and insights are welcomed! Thank you guys.</p>
