Lists and LINQ
Lists and LINQ
2024, 7:57 PM
Cheatsheets / Learn C#
Lists in C#
In C#, a list is a generic data structure that can List<string> names = new List<string>
hold any type. Use the new operator and
();
declare the element type in the angle brackets
< >. List<Object> someObjects = new
In the example code, names is a list containing List<Object>();
string values. someObjects is a list
containing Object instances.
Generic Collections
about:srcdoc Page 1 of 9
19.01.2024, 7:57 PM
Limitless Lists
Unlike a C# array, a C# list does not have a // Initialize array with length 2
limited number of elements. You can add as
string[] citiesArray = new string[2];
many items as you like.
citiesArray[0] = "Los Angeles";
citiesArray[1] = "New York City";
citiesArray[2] = "Dubai"; // Error!
Count Property
citiesList.Remove("Los Angeles");
Console.WriteLine(citiesList.Count);
// Output: 1
about:srcdoc Page 2 of 9
19.01.2024, 7:57 PM
Contains()
result2 = citiesList.Contains("Cairo");
// result2 is false
LINQ
Using LINQ
about:srcdoc Page 3 of 9
19.01.2024, 7:57 PM
var
Since the type of an executed LINQ query’s var custQuery = from cust in customers
result is not always known, it is common to store
where cust.City ==
the result in an implicitly typed variable using the
keyword var . "Phoenix"
select new { cust.Name,
cust.Phone };
// Query syntax
var custQuery =
from cust in customers
where cust.City == "London"
select cust;
about:srcdoc Page 4 of 9
19.01.2024, 7:57 PM
Where
// Query syntax
var custQuery =
from cust in customers
where cust.City == "London"
select cust;
// Method syntax
var custQuery2 = customers.Where(cust
=> cust.City == "London");
// Result: Customer("Bartleby",
"London")
about:srcdoc Page 5 of 9
19.01.2024, 7:57 PM
From
In LINQ queries, the from operator declares a string[] names = { "Hansel", "Gretel",
range variable that is used to traverse the
"Helga", "Gus" };
sequence. It is only used in query syntax.
In the example code, n represents each
element in names . The returned query only var query =
contains those elements for which
from n in names
n.Contains("a") is true.
where n.Contains("a")
select n;
Select
// Method syntax
var treeQuery2 = names.Select(t =>
t.ToUpper());
about:srcdoc Page 6 of 9
19.01.2024, 7:57 PM
You can use a foreach loop to iterate over string[] names = { "Hansel", "Gretel",
the result of an executed LINQ query.
"Helga", "Gus" };
In the example code, query is the result of a
LINQ query, and it can be iterated over using
foreach . name represents each element in var query = names.Where(n =>
names . n.Contains("a"));
Count()
The result of an executed LINQ query has a string[] names = { "Hansel", "Gretel",
method Count() , which returns the number
"Helga", "Gus" };
of elements it contains.
In the example code, Count() returns 2
because the resulting query contains 2 var query = names.Where(x =>
elements containing “a”.
x.Contains("a"));
Console.WriteLine(query.Count());
// Output: 2
Object Initialization
about:srcdoc Page 7 of 9
19.01.2024, 7:57 PM
Remove()
result2 = citiesList.Remove("Cairo");
// result2 is false
Clear()
All elements of a list can be removed with the List<string> citiesList = new
Clear() method. It returns nothing.
List<string> { "Delhi", "Los Angeles",
In the example code, the list is initialized with
three items. After calling Clear() , there are "Kiev" };
zero items in the list. citiesList.Clear();
Console.WriteLine(citiesList.Count);
// Output: 0
about:srcdoc Page 8 of 9
19.01.2024, 7:57 PM
List Ranges
Print Share
about:srcdoc Page 9 of 9