Link to home
Start Free TrialLog in
Avatar of AlHal2
AlHal2Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Record date and time that an item was added to a C# dictionary

I'm using C# in Visual Studio 2017 and have a generic dictionary <string, Widget>
Without changing the widget class is it possible for the dictionary to store when each key value pair was added?
If it was added more than a minute ago then delete it or at least ignore it.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How about storing a 'WidgetHelper' in the dictionary instead.  That is a class based on a Widget with the extra info you require.
As Bill says - with your current situation the answer is no.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlHal2

ASKER

Thanks all.  Would it be worth creating a class that inherits from Widget then using that derived class instead of Widget?
I would think you'd go the other way around. Otherwise, every class you create in the future which has the same kind of requirement will require two classes. If you flip your inheritance the other way--where Widget inherits a base--then you have only one class to inherit from for all future classes. However, this does box you into a particular inheritance hierarchy, which can also be problematic. Using a generic class like what I described above can give you a bit of flexibility in not having to define an inheritance hierarchy just for the purpose of recording the time.
Avatar of AlHal2

ASKER

Thanks.