Start New Discussion within our Software Development Community

Hi, I have a this code which works OK:

    class c <T>

    {
   public T method (T p)
    {
    // ...
    }

    }

but I would need this:

    class C <T, U>
    {

   public U method (T p)
    {
    // ...

    }

    }

How could I write it, compiler reports error?

I cant understand what is your problem because you could use multiple generics as you wrote above.

for example:

    public class Node<K, T>
    {
        public K Key;
        public T Item;
        public Node<K, T> NextNode;
        public Node()
        {
            Key = default(K);
            Item = default(T);
            NextNode = null;
        }

        public Node(K key, T item, Node<K, T> nextNode)
        {
            Key = key;
            Item = item;
            NextNode = nextNode;
        }
    }

        public class LinkedList<K, T>
    {
        Node<K, T> m_Head;
        public LinkedList()
        {
            m_Head = new Node<K, T>();
        }

        public void AddHead(K key, T item)
        {
            Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
            m_Head.NextNode = newNode;
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LinkedList<int, string> list = new LinkedList<int, string>();
            list.AddHead(123, "Sample");
        }
    }
This article has been dead for over six months. Start a new discussion instead.