Čítať v angličtine Upraviť

Zdieľať cez

Learn to manage data collections using the generic list type

Create lists

Run the following code in the interactive window. Select the Enter focus mode button. Then, type the following code block in the interactive window (replace <name> with your name) and select Run:

C#
var names = new List<string> { "<name>", "Ana", "Felipe" };
foreach (var name in names)
{
  Console.WriteLine($"Hello {name.ToUpper()}!");
}

If you are running this on your environment, you should follow the instructions for the local version instead.

You've created a list of strings, added three names to that list, and printed out the names in all CAPS. You're using concepts that you've learned in earlier tutorials to loop through the list.

The code to display names makes use of the string interpolation feature. When you precede a string with the $ character, you can embed C# code in the string declaration. The actual string replaces that C# code with the value it generates. In this example, it replaces the {name.ToUpper()} with each name, converted to capital letters, because you called the String.ToUpper method.

Let's keep exploring.

Try the code in your browser