{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Arrays, Lists, and Collections\n", "\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=qLeF_wpnVto&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=12) for this module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays, lists, and collections can be pretty useful. Try looking at a list:\n", "> Run the following code. Does it print out what you expected?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello !\r\n", "Hello ANA!\r\n", "Hello FELIPE!\r\n" ] } ], "source": [ "using System;\n", "using System.Collections.Generic;\n", "\n", "var names = new List { \"\", \"Ana\", \"Felipe\" };\n", "foreach (var name in names)\n", "{\n", " Console.WriteLine($\"Hello {name.ToUpper()}!\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## So what does that code mean?\n", "\n", "- **System.collections.Generic**: This is a namespace that has lists in it. If you don't tell the code that you're using it, you have to write \"Systems.Collections.Generic.List\" every time you want to use a list. This saves some typing!\n", "- **var**: It's what you put when you have a variable, but don't know/care what the variable type is.\n", "- **List\\**: This means that you're making a list of strings. In place of `string`, you can put in `int`, `double`, or any other variable.\n", "- **foreach**: This is another for loop! It goes through every item in a list.\n", "- **name in names**: This is a style that a lot of people prefer. `names` is the whole list that contains plural names. `name` is an individual item in `names`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Alternative method\n", "\n", "The previous code is a bit more clear to read for human than the code below, but the code below has some more recognizable code, based off of what we've learned.\n", "These are really just two different styles of writing the same code. Feel free to use whatever makes the most sense to you!\n", "> Run the following code.\n", ">\n", "> Can you identify similar parts of code between the two different methods?\n", ">\n", "> Which method do you prefer?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello \r\n", "Hello ANA\r\n", "Hello FELIPE\r\n" ] } ], "source": [ "using System;\n", "using System.Collections.Generic;\n", "\n", "var names = new List { \"\", \"Ana\", \"Felipe\" };\n", "for (int i = 0;i < names.Count; i++)\n", "{\n", " Console.WriteLine($\"Hello {names[i].ToUpper()}\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add\n", "\n", "You can add names to lists pretty easily. Lists have the method `Add()`, which tacks on a new item to the end of the list.\n", "> Run the code.\n", ">\n", "> Then try adding your own name instead." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello !\r\n", "Hello ANA!\r\n", "Hello FELIPE!\r\n", "Hello SOPHIA!\r\n" ] } ], "source": [ "var names = new List { \"\", \"Ana\", \"Felipe\" };\n", "\n", "names.Add(\"Sophia\");\n", "\n", "foreach (var name in names)\n", "{\n", " Console.WriteLine($\"Hello {name.ToUpper()}!\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Remove\n", "\n", "You can also remove names. Try that out:\n", "\n", "> Run the code.\n", ">\n", "> Then choose which name you want removed.\n", ">\n", "> What happens when you try to remove something that isn't there?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello ANA\r\n", "Hello FELIPE\r\n" ] } ], "source": [ "var names = new List { \"\", \"Ana\", \"Felipe\" };\n", "\n", "names.Remove(\"\");\n", "\n", "for (int i = 0;i < names.Count; i++)\n", "{\n", " Console.WriteLine($\"Hello {names[i].ToUpper()}\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Printing a specific item\n", "\n", "What if you don't want to print out all of your friends? What if you just want to print out one friend? That's where brackets come in.\n", "> Run the code.\n", ">\n", "> Try printing a different spot in the list.\n", ">\n", "> Do you need a 0 or 1 to print the first item in a list?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sophia\r\n" ] } ], "source": [ "var names = new List { \"\", \"Sophia\", \"Felipe\" };\n", "Console.WriteLine(names[1]);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Don't forget that lists are \"0\" based. The first spot is the \"0th\" spot." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Playground\n", "\n", "Play around with what you've learned! Here's some starting ideas:\n", "> Make a list of groceries you need called `groceries`.\n", ">\n", "> Can you print out \"buy \\!\" for every item?\n", ">\n", "> What's the 3rd item of the list? Is that the same as `groceries[3]?`\n", ">\n", "> Make sure to add \"toothpaste\".\n", ">\n", "> Remove your least favorite item.\n", ">\n", "> Now what's third on the list?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Playground\r\n" ] } ], "source": [ "Console.WriteLine(\"Playground\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "> [⏩ Next Module -Search, Sort, and Index Lists](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/11-%20Search%2C%20Sort%2C%20and%20Index%20Lists.ipynb)\n", ">\n", "> [⏪ Last Module - Combining Branches and Loops](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/09-Combining%20Branches%20and%20Loops.ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=qLeF_wpnVto&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=12)\n", ">\n", "> [Documentation: Arrays, Lists, and Collections](https://docs.microsoft.com/dotnet/csharp/tour-of-csharp/tutorials/arrays-and-collections?WT.mc_id=Educationalcsharp-c9-scottha)\n", ">\n", "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other resources\n", "\n", "Here's some more places to explore:\n", "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }