list of objects c# code example
Example 1: c# lists
using System.Collections.Generic
var myList = new List<int>();
List<int> myList = new List<int>();
List<int> myList = new List<int>() {2, 5, 9, 12};
string myString = "Hello"
List<char> myList = new List<char>(myString);
var myFirstList = new List<int>() {9, 2, 4, 3, 2, 1};
var mySecondList = new List<int>(myFirstList);
myList.Add(4);
myList.Insert(0,3)
myList.AddRange(new int[3] {3, 5, 5, 9, 2});
for (int i = 0; i < myList.Count; i++)
{
if ( myList[i] == 5)
{
myList.Remove(myList[i]);
i--;
}
}
myList.Clear();
List<List<int>> myList = new List<List<int>>(){
new List<int>() {1,2,3},
new List<int>() {4,5,6},
new List<int>() {7,8,9}
};
Console.WriteLine(myList.ElementAt(0).ElementAt(1));
Example 2: C# list
List<string> myListOfStrings = new List<string>
{
"this",
"is",
"my",
"list"
};
Example 3: how to create a list c#
C# By Magnificent Mamba on Dec 23 2019
IList<int> newList = new List<int>(){1,2,3,4};
Example 4: c# new list object
var intList = new List<int>();
Example 5: c# get list object type of generic list
Type type = pi.PropertyType;
if(type.IsGenericType && type.GetGenericTypeDefinition()
== typeof(List<>))
{
Type itemType = type.GetGenericArguments()[0];
}
Example 6: c# list of properties from list of objects
List<string> firstNames = people.Select(person => person.FirstName).ToList();