Net-informations.com

How to use C# List Class

C# List Class

c# list example

The Collection classes are a group of classes designed specifically for grouping together objects and performing tasks on them. List class is a collection and defined in the System.Collections.Generic namespace and it provides the methods and properties like other Collection classes such as add, insert, remove, search etc. It's the replacement for arrays, linked lists, queues, and most other one-dimensional data structures. This is because it has all kinds of extra functionality, including the ability to grow in size on-demand .

The C# List class represents a strongly typed list of objects that can be accessed by index and it supports storing values of a specific type without casting to or from object.

C# List

How to add items in List collection C#?

The parameter T is the type of elements in the list.

Add elements in List collection

Add Integer values in the List collection

Add String values in the List

Count list elements

How to find the size of a list?

How to count number of items in a List c#?

You can use count property to know the number of items in the List collection or the length of a C# List

Retrieve List elements

How to retrieve items from List c#?

You can retrieve items from List collection by using for loops.

foreach loop

for loop

Insert List elements

How to insert item in the List c#?

You can use insert(index,item) method to insert an in the specified index.

In the above code the color "violet" inserted in the index position 1.

How to sort a C# List

You can use the sort() method of C# List for ordering items in the List.

How to remove an item from List collection ?

Remove() can use to remove item from List collection.

How to check if an item exist in the List collection ?

You can use List.Contains() methods to check an item exists in the List

How to copy an Array to a List collection ?

How to Convert List to String in C#

You can convert a C# List to a string use the following method.

The output look like "Red,Blue,Green"

You can replace the seperator to any character instead of ","

Convert List to Array in C#

You can convert a C# List to an Array using toArray() method.

How to empty a list in C#?

Finally clear method remove all the items from List collection.

List Vs Array

Arrays are memory-efficient . Lists are built on top of arrays. Because of this, Lists use more memory to store the same data. Array is useful when you know the data is fixed length , or unlikely to grow much. It is better when you will be doing lots of indexing into it, i.e. you know you will often want the third element, or the fifth, or whatever. On the other hand, if you don't know how many elements you'll have, use List. Also, definitely use a List any time you want to add/remove data, since resizing arrays is expensive.

If you are using foreach then the key difference is as follows:

Array:

  1. no object is allocated to manage the iteration
  2. bounds checking is removed

List via a variable known to be List :

  1. the iteration management variable is stack allocated
  2. bounds checking is performed

Last Updated: 01-01-2021

List Vs ArrayList

c# List Vs ArrayList

In C# List is depend by array so the theoretical limit of size would be the limit of the array's capacity. Appending elements is efficient because we are using the free slots at the end , but inserting elements can be slow because all elements in the List after the insertion point have to be shifted to make a free slot. In case of searching, it is is efficient if the BinarySearch method is used on a list that has been sorted, if you use any other search algorithm is inefficient because each item must be individually checked.

How to get duplicate items from a list using LINQ

The GroupBy keyword groups the elements that are the same together, and the Where keyword filters out those that only appear once, leaving you with only the duplicates .

Difference between list and dictionary in C#

Both lists and dictionaries are used to store collections of data. List (ICollection ) is simply a set of items and Dictionary(IDictionary) is a set of key-value pairs. The essential difference therefore is in how the containers are indexed. If you want to know more details..... what is the difference between list and dictionary ?

The following C# program shows the implementation of the above functionalities in List collection.










net-informations.com (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About