C # string[], int[] array usage and distinction

Source: Internet
Author: User
Tags abstract arrays

Often with string[], int[] These arrays, today by chance found another System.Array class, which declares the following:

The code is as follows Copy Code

Public abstract class Array:icloneable, IList, ICollection, IEnumerable, IStructuralComparable, istructuralequatable

It seems to be an abstract class.

Check out the MSDN and find out what it does for the following purposes:

"Provides methods for creating, manipulating, searching, and sorting arrays, and thus serves as the base class for all arrays in the common language runtime. "
The array class is the base class for language implementations that support arrays. However, only systems and compilers can derive explicitly from the Array class. The user should use the array constructs provided by the language. "

Write your own code to try, indeed, as mentioned above, cannot inherit array this abstract class,

Examples of how array is used:

  code is as follows copy code

             Array ABC = array.createinstance (typeof (String), 3);
            ABC. SetValue ("Lzd1", 0);
            ABC. SetValue ("Lzd2", 1);
            ABC. SetValue ("Lzd3", 2);
            for (int i = 0; i < ABC. Length; i++)
                 Console.WriteLine (ABC. GetValue (i));

Look at this code, feel and string[], int[] The essence is the same, is cumbersome, no wonder MSDN said it is the base class of all arrays, but with refactor translation of a paragraph string[],int[] code, found not translated into an array, Do not know how the interior of the conversion between them, regardless of this, anyway, the most important to think about the role and use of the array where? After all, directly defined arrays are much simpler than array.createinstance ().

Try to understand that a directly defined array does not support sorting, and if you define string[], int[, and you want to sort, you can use the Sort method in the array abstract class, which I think is very useful.

The code is as follows Copy Code
string[] def = new string[] {"Lzd7", "Lzd5", "Lzd6"};
for (int i = 0; i < def. Length; i++)
Console.WriteLine (Def[i]);
Array.Sort (Def);
for (int i = 0; i < def. Length; i++)
Console.WriteLine (Def[i]);
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: 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.