数组

1.一维数组:
语法: 数据类型[] 数组名;
但是,这 样的命名是:数据类型 数组名[] 错误的。

  1. 在C#中我们可以通过下标,来访问数组的元素。语法:数组名[下标]
  2. 通过数组的length属性,可以获得数组的长度。语法:数组名.Length.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShuZu
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[5] ;
            int[] array = new int[5] { 0, 1, 2, 3, 4 };
            int[] array1 = new int[] { 0, 1, 2, 3, 4 };  //省略数组长度
            int[] array2 = { 0, 1, 2, 3, 4 };            //省略new关键字
            Console.WriteLine("数组array的元素个数是{0}",array.Length);
            Console.WriteLine("数组array1的元素个数是{0}", array1.Length);
            Console.WriteLine("数组array2的元素个数是{0}", array2.Length);
        }
    }
}

在这里插入图片描述
2.对象数组:
对象数组就是数组里的每个元素都是类的对象,赋值时先定义对象,然后将对象直接赋给数组就行了。

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

namespace ShuZu
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义对象数组
            Student[] student = new Student[3];
            //为对象数组元素赋值
            student[0] = new Student();
            student[0].name = "张海";
            student[0].score = 98;
            student[1] = new Student();
            student[1].name = "李成";
            student[1].score = 96;
            student[2] = new Student();
            student[2].name = "江一";
            student[2].score = 96;
            foreach (Student stu in student)
            {
                stu.showInfo();
            }
            Console.Read();
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值