"Learning Notes" C # List

Source: Internet
Author: User

Tag: Std Space name Lin Specifies to perform the Star Line pre

  • List
    • List is a strongly typed listing
    • List is better than ArrayList and is type-safe in most cases
  • The difference between ArrayList and List
    • 1. ArrayList no restrictions on the type of element
    • 2. Because ArrayList has no restrictions on the types of elements, these elements are stored as Object type objects
    • 3. ArrayList is less efficient when used
  • List usage
    • Adding elements using the Add () method
    • Inserting elements using the Insert () method
    • Insert the string "Star" into the position labeled 1
    • Remove the specified element using the Remove () method
    • Use the RemoveAt () method to delete an element that specifies the location of the subscript
    • Use the Count property to get the number of elements in the current list
    • Use the contains () method to determine whether the specified element exists in the list
    • You can use the subscript to access the elements in the list
    • Empty the list only with clear ()
  •   
    1 usingSystem;2 usingSystem.Collections;3 //using generic collections requires first introducing namespaces4 usingSystem.Collections.Generic;5 6 namespaceListdemo7 {8     class Program9     {Ten         Static voidMain (string[] args) One         { A             //1. Declaring a List object arr -list<string> arr =Newlist<string>(); -             //There are no restrictions on element types in ArrayList, list has restrictions on element types the             //adding elements using the Add () method -Arr. ADD ("Hello"); -Arr. ADD (" World"); -Arr. ADD ("Li"); +  -             //inserting elements using the Insert () method +             //Insert the string "Star" into the position labeled 1 AArr. Insert (1,"Star"); at  -             //Remove the specified element using the Remove () method -Arr. Remove ("Hello"); -             //Use the RemoveAt () method to delete an element that specifies the location of the subscript -Arr. RemoveAt (1); -  in             //use the Count property to get the number of elements in the current list -             intc =arr. Count; to  +             //use the Contains () method to determine whether the specified element exists in the list -             BOOLb = arr. Contains ("Star"); the  *             if(b) $             {Panax NotoginsengConsole.WriteLine ("Star exists in the list"); -             } the             Else +             { AConsole.WriteLine ("Star does not exist in list"); the             } +             //you can use the subscript to access the elements in the list -arr[0] ="Hi there! "; $             stringstr = arr[1]; $ Console.WriteLine (str); -  -             //empty the list only with clear () the arr. Clear (); - Wuyi             //the difference between ArrayList and List the             //1. ArrayList no restrictions on the type of element -ArrayList A =NewArrayList (); WuA.add ("Hello"); -A.add ( -); AboutA.add (154.22f); $  -             //2. Because ArrayList has no restrictions on the types of elements, these elements are stored as Object Type objects -             strings = convert.tostring (a[0]); -             stringS1 = (string) a[0]; A             //A[0] is an object type +  the             //3. ArrayList is less efficient when used -         } $     } the}

"Learning Notes" C # List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.