1

Trying to use ToUpper and ToLower validation in a C# Console Application to verify if a user defined string is inside a Dictionary with string and int elements inside it, printing out the elements with ConstainsKey afterward. For example (simplified):

Dictionary<string, int> example = new Dictionary<string, int>();

//int for number of each type of element
int elements = 3;

for (int i = 0; i < elements; i++)
{
     Console.WriteLine("What is the key?");
     string stringElement = Console.ReadLine();
     
     //Validation loop
     while (string.IsNullOrWhileSpace(stringElement))
     {
          Console.WriteLine("error");
          Console.WriteLine("What is the key?");
          stringElement = Console.ReadLine();
     }
     //Ask user for value
     Console.WriteLine("What is the value?");

     string numElementString = Console.ReadLine();

     while (string.IsNullOrWhiteSpace(intElementString) || (!(int.TryParse(numElementString, out numElement))) || numElement <= 0)
     {
          Console.WriteLine("error");
          Console.WriteLine("What is the value?");
          numElementString = Console.ReadLine();
     }
     example.Add(stringElement, numElement);
}

//What does user want to have printed
Console.WriteLine("What elements are you looking for? Please type in the key you need.");

//Catch key user answers
string userAnswer = Console.ReadLine();

//userAnswer validation look for IsNullOrWhiteSpace
while (string.IsNullOrWhiteSpace(userAnswer))
{
     Console.WriteLine("error");
     Console.WriteLine("What elements are you looking for? Please type in the key you need.");
     userAnswer = Console.ReadLine();

I got that far, I'm just having an issue with using the ToUpper and ToLower expressions to have the console print out the specific <string, int> variables that are inside the dictionary the user wants from the specified user input. Any push in the right direction will help... just want a simple solution for now since I'm still getting into the programming language.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
K-Tech
  • 21
  • 1
  • Dictionary keys are case sensitive. How is ToUpper and ToLower going to be useful in retrieving an element, since you don't store the element using ToUpper or ToLower? Why don't you start by retrieving an element based on its key and displaying the value? – Honeyboy Wilson Aug 23 '20 at 20:21
  • Does this answer your question? [Case-INsensitive Dictionary with string key-type in C#](https://stackoverflow.com/questions/13988643/case-insensitive-dictionary-with-string-key-type-in-c-sharp) Or this one? https://stackoverflow.com/q/13230414/5803406 – devNull Aug 23 '20 at 22:42

1 Answers1

2

You don't need to use ToUpper or ToLower to try to find a key in a case-insensitive way. Instead, you can pass a comparer to the constructor of the dictionary that specifies that it should ignore case when adding or retrieving items:

var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

Now, when you search for a value, it will do a case-insensitive comparison to try to find it:

// Add a value using lower case key
dict.Add("foo", "bar");  

// Search for a value using upper case returns 'true'
if (dict.ContainsKey("FOO")) { } 

And trying to add a key with a different case will throw an ArgumentException with the message "An item with the same key has already been added."

dict.Add("Foo", "bar2")  // Argument exception - key already exists
Rufus L
  • 36,127
  • 5
  • 30
  • 43