Certainly! In C#, you can add items to a list using the Add method. Here's a simple example:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create a list of integers
List<int> myNumbers = new List<int>();
// Add items to the list
myNumbers.Add(10);
myNumbers.Add(20);
myNumbers.Add(30);
// Display the contents of the list
Console.WriteLine("List contents:");
foreach (var number in myNumbers)
{
Console.WriteLine(number);
}
}
}
In this example, we create a list of integers (List<int> myNumbers) and use the
Add method to add three numbers (10, 20, and 30) to the list. The
foreach loop is then used to iterate through the list and display its contents.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Certainly! In C#, you can add items to a list using the Add method. Here's a simple example:
In this example, we create a list of integers (List<int> myNumbers) and use the Add method to add three numbers (10, 20, and 30) to the list. The foreach loop is then used to iterate through the list and display its contents.