C#_ Array

Source: Internet
Author: User

1: An array is a set of objects of the same type. In the C # language, an array is derived from the abstract class System.Array, which is a reference type.

(1) One-dimensional arrays

Examples of defining one-dimensional arrays:

int[]arr;//defines a one-dimensional array, arr, whose array elements are of type int string[]str;//defines a one-dimensional array str, each of which is of type string

After the array is defined, the data must be initialized before it can be used. There are two ways to initialize an array.

The first type of initialization allows you to specify the complete contents of an array literally, as in the following example:

The INT[]ARR={1,3,5,7,9};//ARTR array has 5 elements, and the values of the elements are sequentially 1,3,5,7,9

The second type of initialization allows you to specify the array size, and then initialize all the array elements with the new keyword, as shown in the following example:

Int[]arr=new int[5];//uses the new display to initialize the array, each array element is given the same default value,                            //For numeric types, the default value is 0int[]arr=new int[5]{1,3,5,7,9};//uses the new keyword, Each element of the array is assigned a value of 1,3,5,7,9,                                            //In this way, and the array size must be the same as the number of elements.

(2) Two-dimensional arrays

An example of a two-dimensional array is defined as follows:

int[,]arr;//defines a two-dimensional array, arr, whose array elements are of type int string[,]str;//defines a two-dimensional array str, each of which is of type string

Similar to an array, two-dimensional array initialization can take the form of the following:

int[,]arr={{1,3,5},{7,9,11}};//defines an array of two rows of three columns arr, and initializes an array of int[,]arr=new int[2][3];//that each array element is initialized to 0int[,]arr=new int[2] [3] {{1.3.5},{7,9,11}};

2: The sample code for the array application is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace consoleapplication35{    class program    {        static void Main (string[] args)        {            int[] arr = new INT[5] {4, 9, -3, 2, ten};            int i;            int min = arr[0];            for (i = 1; i < arr. Length; i++)            {                if (min > Arr[i])                    min = arr[i];            }            Console.WriteLine ("The minimum value for the array arr is" + min.) ToString ());            Console.readkey ();}}}    

The Console.WriteLine in the program ("The minimum value in the array arr is" + min.) ToString ()); "+" in the string join operator, Min. ToString () indicates that the number represented by Min is converted to a string.

Operation Result:

C#_ Array

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.