C# Programming Language: Lists

Creating List

using System.Collections.Generic;
List<int> myList = new List<int>();

Adding Elements Into The List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
Console.WriteLine(String.Join(" ", myList));
Output:
1 2 3

Updating an Element in The List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList[1] = 4;
Console.WriteLine(String.Join(" ", myList));
Output:
1 4 3

Inserting Elements Into The List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Insert(0, 4);
Console.WriteLine(String.Join(" ", myList));
Output:
4 1 2 3

Removing Elements by Value From a List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Remove(2);
Console.WriteLine(String.Join(" ", myList));
Output:
1 3

Removing Elements by Index From a List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.RemoveAt(2);
Console.WriteLine(String.Join(" ", myList));
Output:
1 2

Clearing a List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Clear();
Console.WriteLine(String.Join(" ", myList));
Output:

Checking Elements in a List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
Console.WriteLine(myList.Contains(2));
Console.WriteLine(myList.Contains(4));
Output:
True
False

Checking the Size of a List

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);
Console.WriteLine(myList.Count);
Output:
3

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

NuNet Development Update: May 2022

Active Record — Under the Hood

How to create a Setup so that you can ping google but not able to ping Facebook from same system.

The compilation process of a c source file

Azure Subscriptions shenanigans

Hire Dedicated Objective-C Developers

InvArch Weekly Updates

A complete guide to deploy a full-stack application based on Django and VueJS: Django and Vue…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jesse L

Jesse L

More from Medium

C# Programming Language: Queue

【C#】Six principles of object orientation programming

Scoped, Transient and Singleton

Async programming vs Parallel programming (Basic)