我有一堂课

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

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


我想知道如何实现这一目标?我需要一种方法来创建此类的实例,该实例将int,string,String,double等存储到GenericType变量中。

最佳答案

您可以使您的类通用:

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

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

10-07 21:24