C#的数据类型和集合初体验

User.cs

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

namespace _20200406afternoon
{
    class User
    {
        //字段
        private string loginId;
        private string passWord;
        //属性
        public string LoginID
        { get { return this.loginId; }
            set { this.loginId = value; }
        }
        public string PassWord
        {
            get { return this.passWord; }
            set { this.passWord = value; }
        }
        //构造函数
        public User()
        {

        }
        public User(string LoginId,string PassWord)
        {
            this.LoginID = LoginId;
            this.PassWord = PassWord;
        }
        //方法
        public void PrintUserInfo()
        {
            Console.WriteLine("用户名{0},密码{1}",loginId,passWord);
        }
    }
}
UserList.cs

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

namespace _20200406afternoon
{
    /*需求
     * User u1 = new User();
     *数组初始化 必须指定大小
     * User[] userArray = new User[?];
     * 读取元素  必须通过 索引
     * userArray[?]= user01
     */
    class UserList//用户集合类,对用户数组 进行扩展
    {
        //************字段********************************
        private User[] data;
        private int currentIdex=0;

        //************属性********************************
        public  User[] Users
        {
            get { return this.data; }
            set { this.data = value; }
        }
        /// <summary>
        /// 有效元素个数
        /// </summary>
        public int Capacity 
        {
            get
            {
                return currentIdex;
            }
                }

        //*************构造函数*****************************
        public UserList():this(8)
        {
            //:this(8)调用的是带参数的构造函数,会执行
        }
        public UserList(int capacity)
        {
            data = new User[capacity];
        }
        //方法
        public void Add(User value)
        {
            CheckCapacity();

            data[currentIdex] = value;


        }

        private void CheckCapacity()
        {
            if (currentIdex>=data.Length)
            {
                User[] newData = new User[data.Length * 2];
                //这里有个常用方法,CopyTo
                data.CopyTo(newData, 0);
                data = newData;
            }

        }

        public User GetUser(int index)
        {
            return data[index];
        }
        //插入功能
        public void InserUser(User data)
        { 

        }
        //删除功能
    }
}
Program.cs

using System;
using System.Collections.Generic;

namespace _20200406afternoon
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //每日一练
            //数组初始化 必须制定大小
            UserList list = new UserList(3);
            User u1 = new User();
            list.Add(u1);
            list.Add(new User());
            list.Add(new User());
            list.Add(new User());

            for (int i = 0; i <list.Capacity ; i++)
            {
                User user = list.GetUser(i);
                user.PrintUserInfo();
            }
            //C# 泛型  List<数据类型>  User[]
            // 自己写的Userlist 就是C#自带的泛型的功能,有增删改查 等等功能
            List<User> list02 = new List<User>();
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值