I am writing a library and I have a method that uses a dictionary. The value of the dictionary is unreliable / unsafe, but the key is trusted, and if the end user was given the opportunity to enter an arbitrary key name, then "bad things" can happen.
Therefore, when other developers use this library function, I want to make them know the key name at compile time. So this is allowed:
string userInput = Console.ReadLine(); Dictionary<string, string> something = new Dictionary<string, string>(); something.Add("MyKey", userInput);
Because "MyKey" is a string literal known at compile time. But something like this can cause either compilation or a runtime exception:
string userInput = Console.ReadLine(); string userKey = Console.ReadLine(); Dictionary<string, string> something = new Dictionary<string, string>(); something.Add(userKey, userInput);
Since user input was used for the key (userKey), and thus it was unknown at compile time.
I looked in GetType (), and there is nothing that really distinguishes a literal string and a string created at runtime.
source share