C#基础知识之数组

作用:连续的地址、多个数据
分类:一维、二维、多维数组
语法:数据类型[ ]  数组名
(1)int[] arr = new int[5]{0,1,2,3,4}; 
(2)int[] arr = new int[]{0,1,2,3,4};   // 省略长度
(3)int[] arr = {0,1,2,3,4};                // 省略new 
for (int i = 0; i < arr.Length; i++)
{
    Console.Write(arr[i] + "  ");
}

特点:
(1)定长,不可动态扩展

(2)类型必须与声明相同



对象数组

定义数组元素为对象

例:

public class Student
    {
        public string name;
        public double score;
        public void showInfo()
        {
            Console.WriteLine(name + "\t" + score);
        }
    }

public class Program
    {
        static void Main(string[] args)
        {
            Student[] stus = new Student[2];
            stus[0] = new Student();
            stus[0].name = "张三";
            stus[0].score = 100;
            stus[1] = new Student();
            stus[1].name = "李四";
            stus[1].score = 80;
            foreach (Student item in stus)
            {
                item.showInfo();
            }

}

}

输出结果: 张三   100

                  李四    80

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值