Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@

using System.Text;
var capitals = new Dictionary<string, string>()
{
{"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<string, string>()
{
{"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<string, string>()
{
{"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();
}
}
}
Console.WriteLine($"Index of USA: {orderedCapitals.IndexOf("USA")}");
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -7,35 +8,86 @@ namespace DictionaryTests
[TestClass]
public class DictionaryGetValueByIndexTests
{
private static Dictionary<string, string> CreateCapitals() => new()
{
{"Turkey", "Ankara"},
{"UK", "London"},
{"USA", "Washington"}
};

[TestMethod]
public void GivenDictionary_WhenAccessedByIndex_ThenReturnElement()
{
var capitals = new Dictionary<string, string>()
{
{"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<string, string>()
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<ArgumentOutOfRangeException>(() => 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<KeyNotFoundException>(() => capitals["France"]);
}

[TestMethod]
public void GivenOrderedDictionary_WhenAccessedByIndex_ThenReturnElement()
{
var orderedCapitals = new OrderedDictionary<string, string>(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<string, string>
{
{"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"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.3" />
<PackageReference Include="coverlet.collector" Version="10.1.1" />
</ItemGroup>

</Project>
Loading