static void Main(string[] args) { Dictionary<string, string> dictionary = new Dictionary<string, string>(); StreamReader reader = new StreamReader(@"..\..\Dictionary.txt"); using (reader) { var line = reader.ReadLine(); while (line != null) { var wordsArr = line.Split(new[] {" - "}, StringSplitOptions.RemoveEmptyEntries); var word = wordsArr[0]; var explanation = wordsArr[1]; dictionary.Add(word, explanation); line = reader.ReadLine(); } } var inputWord = Console.ReadLine(); if (inputWord != null && !dictionary.ContainsKey(inputWord)) { Console.WriteLine("NO such word in this dictionary"); } Console.WriteLine("{0} - {1}", inputWord, dictionary[inputWord]); }
#region Methods static void Dict() { Console.Write("enter a word to search: "); string word = Console.ReadLine(); Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add(".NET", "platform for applications from Microsoft"); dict.Add("CLR", "managed execution environment for .NET"); dict.Add("namespace", "hierarchical organization of classes"); try { if (dict.ContainsKey(word)) { Console.WriteLine("{0}", dict[word]); } else { throw new ArgumentOutOfRangeException(); } } catch (ArgumentOutOfRangeException) { Console.WriteLine("The word is not in the dictionary!");
static void Main(string[] args) { Dictionary<string, string> dictionary = new Dictionary<string, string>(); while (true) { Console.WriteLine("Enter word: "); string key = Console.ReadLine(); // For end enter Spacebar if (key == " ") { break; } Console.WriteLine("Enter translation: "); string value = Console.ReadLine(); dictionary.Add(key, value); } foreach (var item in dictionary.Keys) { string word = item; string translation = dictionary[item]; Console.WriteLine("{0} - {1}", word, translation); } }
static void Main(string[] args) { Dictionary<string, string> theDict = new Dictionary<string, string>(); theDict.Add(".NET", "platform for applications from Microsoft"); theDict.Add("CLR" , "managed execution environment for .NET"); theDict.Add("namespace", "hierarchical organization of classes"); theDict.Add("Gosho Otpochivka", "Bulgarian retard"); theDict.Add("BigSha", "Bulgarian hip-hop singer wannabe"); theDict.Add("Kilata Maika", "Just kilata"); theDict.Add("The Priest from Hisarya", "dunno"); Console.WriteLine("Dictionary entries:"); foreach (var item in theDict) { Console.WriteLine(item.Key); } Console.WriteLine("Type a key to get the explanation:"); string key = Console.ReadLine(); string input; if (theDict.TryGetValue(key, out input)) { Console.WriteLine(input); } else { Console.WriteLine("wrong key! Try again"); } }