Untitled
unknown
plain_text
a year ago
1.2 kB
10
Indexable
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Define a list of car brands
List<string> carBrands = new List<string> { "Toyota", "Ford", "Honda", "BMW", "Mercedes" };
// Print the original list
Console.WriteLine("Original list:");
foreach (var brand in carBrands)
{
Console.WriteLine(brand);
}
// Add a new car brand to the list
carBrands.Add("Audi");
Console.WriteLine("\nList after adding Audi:");
foreach (var brand in carBrands)
{
Console.WriteLine(brand);
}
// Access an element by index
Console.WriteLine("\nCar brand at index 2: " + carBrands[2]);
// Modify an element in the list
carBrands[1] = "Chevrolet";
Console.WriteLine("\nList after modifying element at index 1:");
foreach (var brand in carBrands)
{
Console.WriteLine(brand);
}
// Remove an element from the list
carBrands.Remove("BMW");
Console.WriteLine("\nList after removing BMW:");
foreach (var brand in carBrands)
{
Console.WriteLine(brand);
}
}
}
Editor is loading...
Leave a Comment