使用数组>数组的长度获取,数组的遍历

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//获取数组长度
            Console.WriteLine("Hello,VS");
            
string[] csharpBooks = { "book1","book2","book3"};
            
int bookLength = csharpBooks.Length;//获取所有维度的元素的个数
            Console.WriteLine("一维数组的长度"+bookLength);

            
string[,] csharpBooks2 = { { "book1""book2","book5" }, { "book3""book4" ,"book6"} };
            
int bookLength2=csharpBooks2.Length;
            Console.WriteLine(
"二维数组的所有元素个数" + bookLength2);
            Console.WriteLine(
"二维数组的指定维度的元素个数" + csharpBooks2.GetLength(0));
            

            
//for循环遍历一维数组
            for (int index = 0; index <= csharpBooks.Length - 1; index++)
            {
                
string csharpBook = csharpBooks[index];
                Console.WriteLine(
"for循环遍历一维数组,循环结果  " + csharpBook);
            }

            
//foreach循环遍历一维数组
            foreach (string csharpBook2 in csharpBooks)
            {
                Console.WriteLine(
"foreach循环遍历一维数组,循环结果  " + csharpBook2);
            }

            



        }
    }
}

相关文章:

  • 2021-06-07
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-01
  • 2022-12-23
  • 2021-07-27
  • 2021-11-12
  • 2021-08-05
  • 2021-09-16
  • 2021-08-20
相关资源
相似解决方案