C # common algorithms: Arrays

Source: Internet
Author: User

The C # array concept has been thoroughly object-oriented, which has greatly reduced the difficulty of using our array structure, and it has been supported. NET platform's garbage collection mechanism. As the C # version continues to update, new data structures derived from the array are increasing. According to the 28 principle, we only need to use 20% of them to solve the 80% problem. But in order to achieve the ultimate, we still need to understand them. This article summarizes some of the array-related data structures and their usage until C # (4.0).

Basic array

string[] fruit = new String[5];  
string[] vegetable = new string[] {"Chinese cabbage", "pepper", "potato", "tomato", "broccoli"};  
Fruit[0] = "orange";  
FRUIT[1] = "banana";  
FRUIT[2] = "Apple";  
FRUIT[3] = "Grape";  
FRUIT[4] = "Lychee";

Multidimensional arrays

string[,] monthplan=new string[30,7];  
Monthplan[0,0]= "A";  
Monthplan[0, 1] = "B";

Jagged array

string[][] Foodenum = new string[7][];  
Foodenum[0] = new STRING[10];  
FOODENUM[1] = new string[9];  
FOODENUM[2] = new String[8];  
FOODENUM[3] = new String[7];  
FOODENUM[4] = new String[6];  
FOODENUM[5] = new STRING[5];  
FOODENUM[6] = new STRING[6];

IEnumerator interface

public static ienumerator<string> Yield ()  
{  
    Yield return "Apple";  
    Yield return "orange";  
    Yield return "banana";  
}  
     
public static void Ienumeratortest ()  
{  
    var iterator=yield ();  
    while (iterator. MoveNext ())  
    {  
        Console.WriteLine (iterator). current);  
    }  
    /*out put 
     apple 
     orange 
     Banana 
     */
}

Array Fragment

public static void ArraySegment ()  
{  
    string[] vegetable = new string[] {"Chinese cabbage", "pepper", "potato", " Tomato "," broccoli "};  
    Count:get data begin the current index.  
    arraysegment<string> arraysegment = new arraysegment<string> (vegetable,2,2);  
    for (int i = Arraysegment.offset I <= arraysegment.offset + arraysegment.count; i++)  
    {  
        Console.WriteLine ( Arraysegment.array[i]);  
    }  
    * * out put 
     * potato 
       tomato 
       broccoli
* * *

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.