C# List Example - Collection Types
In my previous article I explained about the difference between Collection Types - C# Generic List and ArrayList. So, In short an array does not resize dynamically whereas a List does. With it, you do not need to manage the size on your own.
Using List in C#
List is a generic (constructed) type. You need to use <angel bracket> in the List declaration. It may any element like int, string, class etc. But a List can also hold reference types and object instances.
C# List Example Code |
C# List Methods
There are following methods are available in C# List such as Add(), AddRange(), Clear(), Contains(), Exists(), IndexOf() etc. Here I will explain use of these methods in brief.
C# List Add
You can add new item to a List using c# list add() method. The below example adds a int type to a List collection.
static void
Main(string[] args)
{
List<int>
list = new List<int>();
list.Add(11);
list.Add(32);
list.Add(53);
list.Add(74);
}
Note: If we need to add an array to list we use the AddRange() method.
Getting value from C# List
You can read value from a List using list foreach or by for loop. The below shows that how to retrieve values from a List.
foreach
(int num in list) // Loop through List with
foreach
{
Console.WriteLine(num);
}
for (int i = 0; i < list.Count;
i++) // Loop through List
with for
{
Console.WriteLine(list[i]);
}
C# Count List
To get the number of elements in your List, access the Count property. Use the Clear() method, along with the Count property, to erase all the elements in a List.
Convert Array To List - C#
If you need to convert an array to list, find the example below:
int[]
arr = new int[3]; // New array with 3 elements
arr[0] = 21;
arr[1] = 31;
arr[2] = 51;
List<int> list = new List<int>(arr); // Copy to List
Console.WriteLine(list.Count); //
3 elements in List
C# List Find
You can find any item in your list by using foreach loop, find the example below:
// New list for example
List<int> listNum = new List<int>(new int[] { 2, 3, 5 });
// See if List contains 3
foreach (int number in listNum)
{
if (number == 3) // Will match once
{
Console.WriteLine("Contains 3");
}
}
There are some alternative methods available in List like Contains(), Exists(), IndexOf().
- Contains() returns only a bool.
- Exists() receives a Predicate and returns a bool.
- IndexOf() returns the position of the element found.
C# List Join - Convert List<string> to comma-separated string
This is helpful when you need to turn several strings into one comma-delimited string. It requires the ToArray instance method on List. Have a look into this example below:
// List of cities we need to join
List<string> cities = new List<string>();
cities.Add("New Delhi");
cities.Add("London");
cities.Add("Meerut");
cities.Add("Kolkata");
// Join strings into one CSV
line
string line = string.Join(",", cities.ToArray());
Console.WriteLine(line);
C# List GetRange
You can get a range of elements in your List using the GetRange method. Find the example below:
List<string> rivers = new List<string>(new string[]
{
"nanu",
"yamuna", //
River 2
"gangotri", //
River 3
"yamnotri",
"yellow"
});
// Get rivers 2 through 3
List<string> range =
rivers.GetRange(1, 2);
(string river in range)
{
Console.WriteLine(river);
}
Conclusion : It was fun in learning and writing an article on C# List, Using List in C# and C# Convert Array to List. I hope this article will be helpful for enthusiastic peoples who are eager to learn and implement some interesting stuffs in new technology.
Please feel free to comment your opinion about this article or whatever you feel like telling me. Also if you like this article, don't forget to share this article with your friends. Thanks!
C# List Example - Collection Types
Reviewed by Ravi Kumar
on
10:47 PM
Rating:
No comments: