Dictionary with description field [closed]

2 weeks ago 9
ARTICLE AD BOX

The most fitable solution would be implementing class with description and it's value. Since value is mutable we make it with class. That would be much easier than creating new dictionary class with description.

At another hand it's not worth doing it due to in source dictionary return only value by key:
public TValue this[TKey key] { get; set; }
Therefore we wouldn't get description directly and that's not convenient way.

So it's better choosing class.
Here's code:

//Generic class for storing the value with description public class DescribedValue<T> { public string Description { get; } public T Value { get; set; } public DescribedValue(T value, string description) : this(description) { Value = value; } //We might not want to set value (leave null property) thus we set only description for future changes public DescribedValue(string description) { Description = description; } } public class Program { public static void Main(string[] args) { //Dictionary with set of data Dictionary<string, DescribedValue<string>> dictionary = new Dictionary<string, DescribedValue<string>>() { ["Key1"] = new DescribedValue<string>("Value1", "This is something weird"), ["Key2"] = new DescribedValue<string>("Value2", "This is completely different") }; //Printing value of the first pair Console.WriteLine(dictionary["Key1"].Value); //Overwriting data dictionary["Key1"].Value = "Value 4"; //Prints new value Console.WriteLine(dictionary["Key1"].Value); //Display description Console.WriteLine(dictionary["Key1"].Description); } }
Read Entire Article