18. Use of arrays and foreach loops inside C #

Source: Internet
Author: User

In C # , As is the case with Java, there are two ways to create arrays. Only one array is said here.

1 ways to create:

Type [] array name ={ element 1, element 2,...};

For example:

            // declare an array of integers
            int[] array_01={1,2,3,4,5,6};
            // outputs the contents of an array element
             for(inti=0; i<array_01.length;i++)
            {
                Console.WriteLine(Array_01[i]);
            }

Section 2 ways to create:

Type [] array name =new type [ number of elements ];

             // initialize an array
            int[] array_02=New int[6];
array_02[0]=1;
array_02[1]=2;
array_02[2]=3;
array_02[3]=4;
array_02[4]=5;
array_02[5]=6;
            
             for(inti=0; i<array_02.length;i++)
            {
                Console.WriteLine(Array_02[i]);
            }

The Foreach loop is primarily used to output the element values of an array.

foreach ( type variable in array name )

{

Statement

}


For example:

            foreach(intIincharray_02)
            {
                Console.WriteLine(i);
            }


This article is from the "Smile" blog, please be sure to keep this source http://yiyiweixiao.blog.51cto.com/2476874/1970510

18. Use of arrays and foreach loops inside C #

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.