Code Snippets

Code Snippets

Code Snippets by Bill Daugherty II

Code Snippets RSS Feed
 
 
 
 

How to use and create Generic Methods

To create generic methods I find it easier to actually create the method by generating the code from the method I would normally use to call them. (Right click then refactor)

static void Swap(ref T item1, ref T item2)
{
    T temp;
    temp = item1;
    item1 = item2;
    item2 = temp;
}

And the call to this class would be something like this…

private void Form1_Load(object sender, EventArgs e)
{
    string first = "Hi";
    string last = "Bill";

    Swap(ref first,ref last);

    MessageBox.Show(first);
    MessageBox.Show(last);
}

Categories

Tags

Archives