Skip to content

Instantly share code, notes, and snippets.

@mjamesharmon
Created January 17, 2024 05:00
Show Gist options
  • Save mjamesharmon/4c19ae0e19fb660ae5c08414b4d57850 to your computer and use it in GitHub Desktop.
Save mjamesharmon/4c19ae0e19fb660ae5c08414b4d57850 to your computer and use it in GitHub Desktop.
Building a Pokédex with KeyedCollection
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"public class Pokemon {\n",
" \n",
" public Pokemon(string name, int hp, string type) {\n",
" Name=name;\n",
" Type=type;\n",
" HP=hp;\n",
" }\n",
"\n",
" public readonly string Name;\n",
"\n",
" public readonly int HP;\n",
"\n",
" public readonly string Type;\n",
"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"using System.Collections.ObjectModel;\n",
"\n",
"public class Pokedex : KeyedCollection<string,Pokemon> {\n",
"\n",
" override protected string GetKeyForItem(Pokemon pokemon) {\n",
" return pokemon.Name;\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"List<Pokemon> myPokemon = new() {\n",
" new Pokemon(\"Eevee\", 55, \"Normal\"),\n",
" new Pokemon(\"Pikachu\", 35, \"Electric\"),\n",
" new Pokemon(\"Vaporeon\", 130, \"Water\"),\n",
" new Pokemon(\"Lilligant\", 70, \"Grass\"),\n",
" new Pokemon(\"Floragato\", 61, \"Grass\")\n",
"};\n",
"\n",
"Pokedex pokedex = new();\n",
"myPokemon.ForEach(\n",
" pokemon => pokedex.Add(pokemon));\n",
"\n",
"Console.Out.WriteLine(pokedex.Count());\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"ename": "Error",
"evalue": "System.ArgumentException: An item with the same key has already been added. Key: Eevee\n at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)\n at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)\n at System.Collections.ObjectModel.KeyedCollection`2.InsertItem(Int32 index, TItem item)\n at Submission#5.<<Initialize>>d__0.MoveNext()\n--- End of stack trace from previous location ---\n at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)",
"output_type": "error",
"traceback": [
"System.ArgumentException: An item with the same key has already been added. Key: Eevee\n",
" at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)\n",
" at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)\n",
" at System.Collections.ObjectModel.KeyedCollection`2.InsertItem(Int32 index, TItem item)\n",
" at Submission#5.<<Initialize>>d__0.MoveNext()\n",
"--- End of stack trace from previous location ---\n",
" at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)"
]
}
],
"source": [
"pokedex.Add(new Pokemon(\"Sandaconda\", 72, \"Ground\"));\n",
"pokedex.Add(new Pokemon(\"Eevee\", 55 , \"Normal\"));"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"bool havePikachu = pokedex.Contains(\"Pikachu\");\n",
"Console.Out.WriteLine(havePikachu);"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"pokedex.Remove(\"Pikachu\");\n",
"bool havePikachu = pokedex.Contains(\"Pikachu\");\n",
"Console.Out.WriteLine(havePikachu);"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Eevee\n",
"HP: 55\n",
"Normal Type\n"
]
}
],
"source": [
"public static void Print(this Pokemon pokemon) {\n",
" Console.Out.WriteLine($\"{pokemon.Name}\");\n",
" Console.Out.WriteLine($\"HP: {pokemon.HP}\");\n",
" Console.Out.WriteLine($\"{pokemon.Type} Type\");\n",
"}\n",
"\n",
"Pokemon eevee = pokedex[\"Eevee\"];\n",
"eevee.Print();"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---\n",
"Eevee\n",
"HP: 55\n",
"Normal Type\n",
"---\n",
"Vaporeon\n",
"HP: 130\n",
"Water Type\n",
"---\n",
"Lilligant\n",
"HP: 70\n",
"Grass Type\n",
"---\n",
"Floragato\n",
"HP: 61\n",
"Grass Type\n",
"---\n",
"Sandaconda\n",
"HP: 72\n",
"Ground Type\n"
]
}
],
"source": [
"foreach(Pokemon pokemon in pokedex) {\n",
" Console.Out.WriteLine(\"---\");\n",
" pokemon.Print();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div class=\"dni-plaintext\"><pre>2</pre></div><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" pokedex.Where(pokemon=>pokemon.HP > 70).\n",
" Count()\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div class=\"dni-plaintext\"><pre>130</pre></div><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pokedex.Max(pokemon=>pokemon.HP)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"public interface IPokemon {\n",
"\n",
" string Name {get;}\n",
"\n",
" int HP {get;}\n",
"\n",
" string Type {get;}\n",
"\n",
"}\n",
"\n",
"public class WaterPokemon : IPokemon {\n",
"\n",
" private readonly string _name;\n",
" private readonly int _hp;\n",
"\n",
" public WaterPokemon(string name, int hp) {\n",
" _name=name;\n",
" _hp = hp;\n",
" }\n",
"\n",
" public string Name => _name;\n",
"\n",
" public int HP => _hp;\n",
"\n",
" public string Type => \"Water\";\n",
"\n",
"}\n",
"\n",
"public class GrassPokemon : IPokemon {\n",
"\n",
" private readonly string _name;\n",
" private readonly int _hp;\n",
"\n",
" public GrassPokemon(string name, int hp) {\n",
" _name=name;\n",
" _hp = hp;\n",
" }\n",
"\n",
" public string Name => _name;\n",
"\n",
" public int HP => _hp;\n",
"\n",
" public string Type => \"Grass\";\n",
"\n",
"}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"public class Pokedex<TPokemon> : KeyedCollection<string,TPokemon>\n",
" where TPokemon : IPokemon {\n",
"\n",
" override protected string GetKeyForItem(TPokemon pokemon) {\n",
" return pokemon.Name;\n",
" } \n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
" WaterPokemon vaporeon = new WaterPokemon(\"Vaporeon\", 130);\n",
" GrassPokemon lilligant = new GrassPokemon(\"Lilligant\", 70);\n",
" GrassPokemon florogato = new GrassPokemon(\"Floragato\", 61);\n",
"\n",
" Pokedex<WaterPokemon> waterPokemon = new();\n",
" Pokedex<GrassPokemon> grassPokemon = new();\n",
" Pokedex<IPokemon> allPokemon = new();\n",
"\n",
" waterPokemon.Add(vaporeon);\n",
"\n",
" grassPokemon.Add(lilligant);\n",
" grassPokemon.Add(florogato);\n",
"\n",
" allPokemon.Add(vaporeon);\n",
" allPokemon.Add(lilligant);\n",
" allPokemon.Add(florogato);"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment