I want to initialize a string,datetime
dictionary, I want a \"real\" time value for each string. Later I want to insert the strings into drop down list as keys and
Try this:
Dictionary<string, TimeSpan> testDictionary = new Dictionary<string, TimeSpan>()
{
{"1 hour", TimeSpan.FromHours(1) },
{"2 hour", TimeSpan.FromHours(2) },
{"3 hour", TimeSpan.FromHours(3) }
};
or use Add method of Dictionary as below:
Dictionary<string, TimeSpan> testDictionary = new Dictionary<string, TimeSpan>();
testDictionary.Add("1 Hour", TimeSpan.FromHours(1));
var dic = new Dictionary<string, TimeSpan>()
{
{"1 Hour", TimeSpan.FromHours(1)},
{"Two days", TimeSpan.FromDays(2)}
};
Instead of DateTime
you should use TimeSpan
Dictionary<string, TimeSpan> dictionary = new Dictionary<string, TimeSpan>();
dictionary.Add("1 hour", new TimeSpan(1, 0, 0)); //1 hour
dictionary.Add("2 days", new TimeSpan(2, 0, 0, 0));//2 days