In C #, string [] arrays and List & lt; string & gt; mutual conversion of generics and Sort of Array classes

Source: Internet
Author: User

Convert string [] to List <string>:

Common browsing and copying code
String [] str = {"1", "2", "3 "};
List <string> list = new List <string> (str );

Convert from List to string []:

Common browsing and copying code
List <string> list = new List <string> ();
String [] str = list. ToArray ();

The Array class implements the Bubble Sorting of elements in the Array. The Sort () method requires that the elements in the array implement the IComparable interface.
For example, System. Int32 and System. String implement the IComparable interface, so the following Array can use Array. Sort ():

Common browsing and copying code
String [] names = {"Lili", "Heicer", "Lucy "};
Array. Sort (names );
Foreach (string name in names)
{
Console. WriteLine (name );
}

If you use a custom class for the array, you must implement the IComparable interface. This excuse defines a method CompareTo ().

Related Article

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.