Type parameters

Only method, class, interfce, delegate and struct can have type parameters.

Other member can only reference them.

 
using System;
class Wrapper<T>
{
    T t;
    T get()
    {
        return t;
    }
    void set(T t)
    {
        this.t = t;
    }
}

class Test
{
    static void Main()
    {
    }
}
  

The following example takes two generic type parameters.

 
using System;
class Pair<TKey, TValue>
{
    TKey key;
    TValue value;

    TKey getKey()
    {
        return key;
    }

    void setKey(TKey key)
    {
        this.key = key;
    }

    TValue getValue()
    {
        return value;
    }

    void setValue(TValue v)
    {
        this.value = v;
    }
}

class Test
{
    static void Main()
    {
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.