C#HashTable

//引入命名空间
using System.Collections
//Hash对象
Hashtable hash=new Hashtable();
//hash由键和值组成


//HashTest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable hash = new Hashtable();
            //添加数据
            hash.Add(1, "lin");
            hash.Add(2, "bai");
            hash.Add(3, "chuan");
            //访问数据,采用键的方式访问
            Console.WriteLine(hash[1]);
            //采用遍历键集合访问数据
            //var是推断类型,可根据所获得的数据类型进行匹配对应
            var skeys = hash.Keys;  //获取hash键的集合
            foreach (object o in skeys)
            {
                Console.WriteLine("键:{0},值:{1}", o, hash[o]);    //从下往上的方式输出
            }
            //采用遍历器
            var ie = hash.GetEnumerator();//获取一个遍历器
            while (ie.MoveNext())  //从当前行开始读起,依次遍历集合的数据,类似游标
            {
                Console.WriteLine("键:{0},值:{1}", ie.Key, ie.Value);
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/god-for-speed/p/11445082.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值