In C# struct, we can know clearly the purpose of a variable by it's name. For example,
public struct Book
{
public string title;
public string author;
}
Then, i know b.title is a type of string and it's referring to title.
However in C# dictionary, we can only specify the type
Dictionary<string,string> d
How can i make the code more readable such that the key of the dictionary is type of string and it is referring to title, and the value is type of string and it is referring to author? That means, other people can easily know that d["J.R.R. Tolkien"] is a wrong use of the dictionary when reading the code.
EDIT @mike z suggested to use a variable name titleToAuthor to help readability. But my real issue is that in the code there are nested dictionary. E.g.
Dictionary<string, Dictionary<string, string>>,
or even 3 levels
Dictionary<string, Dictionary<string , Dictionary< string , string[] >>>.
We want to keep to convenience of using Dictionary without creating our own class but at the same time we need some way to improve the readability
To add key-value pair in C# Dictionary, firstly declare a Dictionary. IDictionary<int, string> d = new Dictionary<int, string>(); Now, add elements with KeyValuePair.
Get Dictionary Value by Key With [] Method in C# We can get the value in the dictionary by using the key with the [] method in C#. We created a dictionary, mydictionary , with the Dictionary<string, string> class. After that, we retrieved the value of the Key 3 key in mydictionary with the [] method.
Access Dictionary Elements The Dictionary can be accessed using indexer. Specify a key to get the associated value. You can also use the ElementAt() method to get a KeyValuePair from the specified index.
Generic; Step 2: Create a Dictionary using Dictionary<TKey, TValue> class as shown below: Dictionary dictionary_name = new Dictionary(); Step 3: If you want to add elements in your Dictionary then use Add() method to add key/value pairs in your Dictionary.
As suggested by @ScottDorman you could define a new Type TitleAuthorDictionary
that derives from Dictionary<string, string>
, like so:
public class TitleAuthorDictionary : Dictionary<string, string>
{
public new void Add(string title, string author)
{
base.Add(title, author);
}
public new string this[string title]
{
get { return base[title]; }
set { base[title] = value; }
}
}
You could then use the more readable Dictionary Collection, like this:
TitleAuthorDictionary dictionary = new TitleAuthorDictionary();
dictionary.Add("Title1", "Author1");
dictionary.Add(title: "Title2", author: "Author2");
dictionary["Title2"] = "Author3";
With .NET 6 and C# 10, you can now use global using
directive to give type aliases in your project.
Give built-in types their aliases in one single place, e.g. GlobalUsings.cs
.
global using Title = System.String;
global using Author = System.String;
Then use aliases for better readability in your dictionaries.
Dictionary<string, Dictionary<Title, Author>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With