you can use the List
xxxxxxxxxx
using System.Collections.Generic;
// Create a new List of strings
List<string> myList = new List<string>();
// Add items to the list
myList.Add("Item 1");
myList.Add("Item 2");
myList.Add("Item 3");
// Access items in the list
Console.WriteLine(myList[0]); // Output: Item 1
// Loop through the list
foreach (string item in myList)
{
Console.WriteLine(item);
}
This will create a new list of strings, add some items to it, access the items in the list and loop through all the items in the list.
xxxxxxxxxx
using System.Collections.Generic
//type is your data type (Ej. int, string, double, bool...)
List<type> newList = new List<type>();
xxxxxxxxxx
// List with default capacity
List<Int16> list = new List<Int16>();
// List with capacity = 5
List<string> authors = new List<string>(5);
string[] animals = { "Cow", "Camel", "Elephant" };
List<string> animalsList = new List<string>(animals);
xxxxxxxxxx
C# By Magnificent Mamba on Dec 23 2019
IList<int> newList = new List<int>(){1,2,3,4};
xxxxxxxxxx
public class Car
{
private int intYear;
private string strCar;
private DateTime dtRelease;
public Car(int intYear, string strCar, DateTime dtRelease)
{
this.intYear = intYear;
this.strCar = strCar;
this.dtRelease = dtRelease;
}
public int IntYear { get => intYear; set => intYear = value; }
public string StrCar { get => strCar; set => strCar = value; }
public DateTime DtRelease { get => dtRelease; set => dtRelease = value; }
}
static void Main(string[] args)
{
List<Car> objCars = new List<Car>();
objCars.Add(new Car(2022, "BMW", new DateTime(2017, 3, 1).Date));
objCars.Add(new Car(2022, "Honda", new DateTime(2019, 2, 1).Date));
objCars.Add(new Car(2022, "Mercedes", new DateTime(1998, 1, 1).Date));
foreach (Car cars in objCars){
Console.WriteLine("----------------");
Console.WriteLine(cars.IntYear);
Console.WriteLine(cars.StrCar);
Console.WriteLine(cars.DtRelease);
}
}
//Output:
//----------------
//2022
//BMW
//01.03.2017 00:00:00
//----------------
//2022
//Honda
//01.02.2019 00:00:00
//----------------
//2022
//Mercedes
//01.01.1998 00:00:00