jordan
jordan

Reputation: 3546

Generic Parameter. How to pass in to a constructor and then add to a list?

I Have a class

class ExpiryList
{
    protected ConcurrentDictionary <GenericType, DateTime> m_list;

    ExpiryList(GenericType obj, DateTime expiryDate)
    {
        m_list.AddOrUpdate(obj, expiryDate, (x, y) => { return expiryDate; });
    }
}

And i am wondering how to implement this? I need a way to create an instance of this class that stores either int, string, String, double etc... into the GenericType variable.

Upvotes: 3

Views: 772

Answers (2)

carlosfigueira
carlosfigueira

Reputation: 87228

You can make your class generic:

class ExpiryList<TAnyType>
{
    protected ConcurrentDictionary <TAnyType, DateTime> m_list;

    ExpiryList(TAnyType obj, DateTime expiryDate)
    {
        m_list.AddOrUpdate(obj, expiryDate, (x, y) => { return expiryDate; });
    }
}

Upvotes: 5

SLaks
SLaks

Reputation: 887275

You're trying to create a generic class:

public class ExpiryList<T>

Upvotes: 3

Related Questions