diff --git a/collections-dictionary/DictionaryGetValueByIndex/DictionaryGetValueByIndex/DictionaryGetValueByIndex.csproj b/collections-dictionary/DictionaryGetValueByIndex/DictionaryGetValueByIndex/DictionaryGetValueByIndex.csproj index 74abf5c976..5bf6c9b8cf 100644 --- a/collections-dictionary/DictionaryGetValueByIndex/DictionaryGetValueByIndex/DictionaryGetValueByIndex.csproj +++ b/collections-dictionary/DictionaryGetValueByIndex/DictionaryGetValueByIndex/DictionaryGetValueByIndex.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net11.0 enable enable diff --git a/collections-dictionary/DictionaryGetValueByIndex/DictionaryGetValueByIndex/Program.cs b/collections-dictionary/DictionaryGetValueByIndex/DictionaryGetValueByIndex/Program.cs index 7d709be843..ed3de8ca59 100644 --- a/collections-dictionary/DictionaryGetValueByIndex/DictionaryGetValueByIndex/Program.cs +++ b/collections-dictionary/DictionaryGetValueByIndex/DictionaryGetValueByIndex/Program.cs @@ -1,30 +1,48 @@ - -using System.Text; +var capitals = new Dictionary() +{ + {"Turkey", "Ankara"}, + {"UK", "London"}, + {"USA", "Washington"} +}; + +var element = capitals.ElementAt(2); + +Console.WriteLine(element); + +Console.WriteLine($"Just Key: {capitals.ElementAt(2).Key}"); + +Console.WriteLine($"Just Value: {capitals.ElementAt(2).Value}"); + +var outOfRange = capitals.ElementAtOrDefault(5); -namespace DictionaryGetValueByIndex +Console.WriteLine($"Out of range: {outOfRange}"); + +var pairs = capitals.ToArray(); + +for (var i = 0; i < pairs.Length; i++) { - public class Program - { - public static void Main(string[] args) - { - var capitals = new Dictionary() - { - {"Turkey", "Ankara"}, - {"UK", "London"}, - {"USA", "Washington"} - }; + Console.WriteLine($"{i}: {pairs[i].Key}"); +} + +Console.WriteLine($"This is the indexer access of the UK capital: {capitals["UK"]}"); - var element = capitals.ElementAt(2); +capitals.Remove("Turkey"); +capitals.Add("Germany", "Berlin"); - Console.WriteLine(element); +Console.WriteLine($"Index 0 after Remove and Add: {capitals.ElementAt(0)}"); + +var orderedCapitals = new OrderedDictionary() +{ + {"Turkey", "Ankara"}, + {"UK", "London"}, + {"USA", "Washington"} +}; - Console.WriteLine($"Just Key: {capitals.ElementAt(2).Key}"); +Console.WriteLine(orderedCapitals.GetAt(2)); - Console.WriteLine($"Just Value: {capitals.ElementAt(2).Value}"); +orderedCapitals.Remove("Turkey"); +orderedCapitals.Add("Germany", "Berlin"); - Console.WriteLine($"This is indexer access of UK capital: {capitals["UK"]}"); +Console.WriteLine($"Index 0 after Remove and Add: {orderedCapitals.GetAt(0)}"); - Console.ReadLine(); - } - } -} \ No newline at end of file +Console.WriteLine($"Index of USA: {orderedCapitals.IndexOf("USA")}"); diff --git a/collections-dictionary/DictionaryGetValueByIndex/DictionaryTests/DictionaryGetValueByIndexTests.cs b/collections-dictionary/DictionaryGetValueByIndex/DictionaryTests/DictionaryGetValueByIndexTests.cs index 92482cd892..20a04b2b50 100644 --- a/collections-dictionary/DictionaryGetValueByIndex/DictionaryTests/DictionaryGetValueByIndexTests.cs +++ b/collections-dictionary/DictionaryGetValueByIndex/DictionaryTests/DictionaryGetValueByIndexTests.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using System.Collections.Generic; using System.Linq; @@ -7,35 +8,86 @@ namespace DictionaryTests [TestClass] public class DictionaryGetValueByIndexTests { + private static Dictionary CreateCapitals() => new() + { + {"Turkey", "Ankara"}, + {"UK", "London"}, + {"USA", "Washington"} + }; + [TestMethod] public void GivenDictionary_WhenAccessedByIndex_ThenReturnElement() { - var capitals = new Dictionary() - { - {"Turkey", "Ankara"}, - {"UK", "London"}, - {"USA", "Washington"} - }; + var capitals = CreateCapitals(); var element = capitals.ElementAt(2); - Assert.AreEqual( "USA", element.Key); + Assert.AreEqual("USA", element.Key); } - [TestMethod] public void GivenDictionary_WhenAccessedByIndex_ThenReturnElementValue() { - var capitals = new Dictionary() + var capitals = CreateCapitals(); + + var element = capitals.ElementAt(2); + + Assert.AreEqual(capitals["USA"], element.Value); + } + + [TestMethod] + public void GivenDictionary_WhenIndexIsOutOfRange_ThenElementAtThrows() + { + var capitals = CreateCapitals(); + + Assert.ThrowsExactly(() => capitals.ElementAt(5)); + } + + [TestMethod] + public void GivenDictionary_WhenIndexIsOutOfRange_ThenElementAtOrDefaultReturnsDefault() + { + var capitals = CreateCapitals(); + + var element = capitals.ElementAtOrDefault(5); + + Assert.AreEqual(default, element); + } + + [TestMethod] + public void GivenDictionary_WhenKeyIsMissing_ThenIndexerThrows() + { + var capitals = CreateCapitals(); + + Assert.ThrowsExactly(() => capitals["France"]); + } + + [TestMethod] + public void GivenOrderedDictionary_WhenAccessedByIndex_ThenReturnElement() + { + var orderedCapitals = new OrderedDictionary(CreateCapitals()); + + var element = orderedCapitals.GetAt(2); + + Assert.AreEqual("USA", element.Key); + Assert.AreEqual("Washington", element.Value); + } + + [TestMethod] + public void GivenOrderedDictionary_WhenItemRemovedAndAdded_ThenNewItemGoesToTheEnd() + { + var orderedCapitals = new OrderedDictionary { {"Turkey", "Ankara"}, {"UK", "London"}, {"USA", "Washington"} }; - var element = capitals.ElementAt(2); + orderedCapitals.Remove("Turkey"); + orderedCapitals.Add("Germany", "Berlin"); - Assert.AreEqual(capitals["USA"], element.Value); + Assert.AreEqual("UK", orderedCapitals.GetAt(0).Key); + Assert.AreEqual("Germany", orderedCapitals.GetAt(2).Key); + Assert.AreEqual(1, orderedCapitals.IndexOf("USA")); } } -} \ No newline at end of file +} diff --git a/collections-dictionary/DictionaryGetValueByIndex/DictionaryTests/DictionaryTests.csproj b/collections-dictionary/DictionaryGetValueByIndex/DictionaryTests/DictionaryTests.csproj index f72bee824b..7d36b0455c 100644 --- a/collections-dictionary/DictionaryGetValueByIndex/DictionaryTests/DictionaryTests.csproj +++ b/collections-dictionary/DictionaryGetValueByIndex/DictionaryTests/DictionaryTests.csproj @@ -1,17 +1,17 @@ - net6.0 + net11.0 enable false - - - - + + + +