Learn C#: Lists and LINQ
Work with data in C# using lists and LINQ queries
StartKey Concepts
Review core concepts you need to learn to master this subject
Lists in C#
Object Initialization
Generic Collections
Limitless Lists
Count Property
Remove()
Clear()
Contains()
Lists in C#
Lists in C#
List<string> names = new List<string>();
List<Object> someObjects = new List<Object>();
In C#, a list is a generic data structure that can hold any type. Use the new
operator and declare the element type in the angle brackets < >
.
In the example code, names
is a list containing string
values. someObjects
is a list containing Object
instances.
Lists
Lesson 1 of 2
- 1At this point, we assume that you’re familiar with arrays: they’re useful tools for managing large amounts of sequential data. But arrays have their drawbacks: * They have a limited length * You…
- 2A list is a sequential data structure that can hold any type. Like arrays, you can use them to store any sequential information, like the letters of the alphabet, comments on a blogpost, the finish…
- 3Our first way to create lists and add items took multiple lines: List citiesList = new List (); citiesList.Add(“Delhi”); citiesList.Add(“Los Angeles”); We can do it all in one line using _objec…
- 4We can check on the status of our list in two ways. We can find the number of elements in the list using the Count property: List citiesList = new List { “Delhi”, “Los Angeles” }; int numberCiti…
- 7Effectively, we can add an infinite number of items to a list: List numbers = new List (); for (int i = 0 ; i < 1000; i++) { numbers.Add(i); } We can access and edit them using bracket notati…
- 8So far we have added, accessed, and removed single elements in a list. What if we wanted to add, access, or remove multiple elements at once? In the world of lists we call a subsequence of elemen…
- 9Like arrays, we can perform an operation for every element in the list using for and foreach loops. With for loops, make sure to use Count to stay within the bounds of the list. for (int i = 0; …
- 10You’ve done great with lists so far! It’s time to take a look at the bigger picture. Remember the one line we mentioned at the beginning of this lesson? using System.Collections.Generic; The lis…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory