This is the second article on C## collections in the first article I discussed ArrayLists and today I am writing about C## Lists. I have used Lists more than Arrays or ArrayList in the projects I have worked on so far.

Must Learn C## Things -ArrayList

· Introduction ∘ Generic and Non-Generic Collection List · ArrayList ∘ ArrayList Implementations ∘ Creating an…

medium.com

Let’s see where we can use C## List its properties and basic operations to do with lists. The most important thing to understand is the use cases of Lists. when to use lists and when not to. We can get syntax and operations from the internet if we forgot, but we should have an idea of which scenarios we should use them.

List is the generic version of ArrayLists. the in front of the List tells that it’s a generic type. Because it’s a generic type you have to specify a type when declaring a List.

Understand What’s Behind Real-Time Web Apps

This is the first article in the article series where I write on how to use ASP .NET Core SignalR to create real-time…

medium.com

That means List is a strongly typed collection of objects. You can store only one specific type of object in a single List. That type may be an int, string, boolean, or even a Class type. Stored objects can be accessed using the index. List have separate methods to sort, search, and modify/manipulate the List.

Creating a List

Let’s see how we can create a List

In line 9 a list of type is created. And new items are being added using .Add() method.

It’s clear that all the elements that were added are int type.

New items also can be added in the initialization phase as well. Using collection initializer syntax.

var bigCities = new List<string>() {“New York”, “London”, “Mumbai”, “Chicago”};

You can see how we have added custom class objects to the list as well. Objects of the class StudentDetails are stored inside students list.

Adding an Array in a List

we can use AddRange to add arrays to the list and Add lists inside the list. We cant add non-generic types like ArrayList directly inside List. Because in definition List is a generic type.

Access List

Accessing List can be done using iteration. Here I have used 3 common methods to access the Lists

Accessing a List using LINQ

C## Lists implements the IEnumerable interface. It gives the ability to query a list using LINQ query syntax. It provides a single querying interface for different types of data sources.

What is Linq

LINQ is Language Integrated Query that is used to retrieve data from C## collections and data sources. LINQ provides a single querying interface for different types of data sources.

In the below code first Students List is created.You can see List student is of type Student.

Then a separate list of results is queried using LINQ. LINQ query retrieves a list of students where name equal “Bill”.

var result = from s in students      
where s.Name == "Bill"     
select s;

Insert Elements in List

You can insert an item to a specific index using the Insert() . Note that the inserting type must match with the List type

Remove Elements from List

Remove() can be used to remove the first occurrence of the specified element. Use RemoeAt() to remove the element from the specified index.

Check Elements in List

you can check an element inside the list by using Contains() .If the specified item contains inside the list it returns true else false.

#programming #net-core #web-development #c#

Must Learn C#- List<T>
1.15 GEEK