欢迎访问我的博客 https://javascript.shop

Dictionary字典类使用范例

原文发布时间为:2009-11-04 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Content content = new Content();
        Response.Write(content.Fruit(2));
        Response.Write("<br/>");
        Response.Write(content.Fruit(4));
    }
}

public class Content
{
    private Dictionary<int, string> fruit = new Dictionary<int, string>();
    public Content()
    {
        this.fruit.Add(1, "香蕉");
        this.fruit.Add(2, "苹果");
    }

    public string Fruit(int key)
    {
        if (fruit.ContainsKey(key))
        {
            return fruit[key];
        }
        else
        {
            return "未知水果";
        }
    }
}

posted @ 2017-07-13 00:12  孑孓子  阅读(109)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop