How to C# List
How to use C# List Class List < T > Class
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 < T > 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.
List < T >
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 ListCount list elements
How to find the size of a list?
You can use count property to know the number of items in the List collection or the length of a C# ListRetrieve List elements
You can retrieve items from List collection by using for loops.
for loopInsert List elements
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:
no object is allocated to manage the iteration
bounds checking is removed
List via a variable known to be List :
the iteration management variable is stack allocated
bounds checking is performed
Content updated on 2018-10-01 02:12 AMLooking for a C# job ?
There are lot of opportunities from many reputed companies in the world. Chances are you will need to prove that you know how to work with .Net Programming Language. These C# Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of .Net Programming. Here's a comprehensive list of .Net Interview Questions, along with some of the best answers. These sample questions are framed by our experts team who trains for .Net training to give you an idea of type of questions which may be asked in interview.
Go to... 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