List

 What is C# List<T>

  • List<T> class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace
  • List class can be used to create a collection of different types like integers, strings etc. List<T> class also provides the methods to search, sort, and manipulate lists.
  • It is different from the arrays. A List<T> can be resized dynamically but arrays cannot.
  • List<T> class can accept null as a valid value for reference types and it also allows duplicate elements.
  • If the Count becomes equals to Capacity, then the capacity of the List increased automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
  • List<T> class is the generic equivalent of ArrayList class by implementing the IList<T> generic interface.
  • This class can use both equality and ordering comparer.
  • List<T> class is not sorted by default and elements are accessed by zero-based index.
  • For very large List<T> objects, you can increase the maximum capacity to 2 billion elements on a 64-bit system by setting the enabled attribute of the configuration element to true in the run-time environment.

Properties


 
Properties            
 Description 
 Capacity     Gets or sets the total number of elements the internal data structure can hold without resizing.
 Count Returns the total number of elements exists in the List<T>    
 Item Gets or sets the element at the specified index.


Methods


 Method     Usage    
 Add     Adds an element at the end of a List<T>.    
 AddRange     Adds elements of the specified collection at the end of a List<T>.        
 BinarySearch Search the element and returns an index of the element.
 Clear Removes all the elements from a List<T>.
 Contains Checks whether the speciied element exists or not in a List<T>
 Find Finds the first element based on the specified predicate function.
 ForEach Iterates through a List<T>.
 Remove<T>     Removes the first occurrence of a specific object from the List<T>.
 Remove All<T>            Removes all the elements that match the conditions defined by the specified predicate.
 Remove At(Int32) Removes the element at the specified index of the List<T>.
 RemoveRange(Int32,Int32)  Removes a range of elements from the List<T>.
 Reverse() Reverses the order of the elements in the List<T> or a portion of it.
 Sort()Sorts the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements.    
 ToArray() Copies the elements of the List<T> to a new array.


Example:

class CollectionExamples
    {
        public static void Main(String[] args)
        {

            // Creating an List<T> of Integers 
            List<int> _List = new List<int>();

            // Adding elements to List 
            _List.Add(1);
            _List.Add(3);
            _List.Add(4);
            _List.Add(7);
            _List.Add(9);
            _List.Add(18);
            _List.Add(22);

            Console.WriteLine("Elements Present in List:\n");

            int p = 0;

            // Displaying the elements of List 
            foreach (int k in _List)
            {
                Console.Write("At Position {0}: ", p);
                Console.WriteLine(k);
                p++;
            }

            Console.WriteLine(" ");

            // removing the element at index 3 
            Console.WriteLine("Removing the element at index 3\n");
            _List.RemoveAt(4);

            int p1 = 0;

            // Displaying the elements of List 
            foreach (int n in _List)
            {
                Console.Write("At Position {0}: ", p1);
                Console.WriteLine(n);
                p1++;
            }
        }

No comments:

Post a Comment

Please let me know if you have any feedback or doubts.

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...