Dictionary的用法!

1、使用方法简单,配合枚举一起使用,主要介绍Dictionary的使用和取值,本人感觉他用在项目固定状态值搭配使用,这样能做到项目最大重用,而且代码方便管理,具体使用代码如下

    声明枚举变量:

    /// <summary>
/// 试卷状态
/// </summary>
public enum DraftPaperState
{
Closed = 0,//关闭
Edit = 1,//编辑中
Release = 2//发布
}

   向集合中添加值:

        /// <summary>
/// 试卷状态
/// </summary>
/// <returns></returns>
public static Dictionary<DraftPaperState, string> GetDraftPaperState()
{
if (_DraftPaperState == null)
{
_DraftPaperState = new Dictionary<DraftPaperState, string>();
_DraftPaperState.Add(DraftPaperState.Closed, "关闭");
_DraftPaperState.Add(DraftPaperState.Edit, "编辑中");
_DraftPaperState.Add(DraftPaperState.Release, "发布");
}
return _DraftPaperState;
}

  界面中使用:

 Dictionary<DraftPaperState, string> draft = Enums.GetDraftPaperState();
foreach (KeyValuePair<DraftPaperState, string> draftPaperState in draftPaperStates)
{
if ((int)draftPaperState.Key == “aaa”)
{
string keyValue= draftPaperState.Value;
}
}

相信你会使用了吧,那就尝试一下吧!如有不尽人意地方,欢迎纠正。

posted on 2012-03-22 15:56  朝扬  阅读(291)  评论(0编辑  收藏  举报