码迷,mamicode.com
首页 > 其他好文 > 详细

20140818 Dictionary

时间:2014-08-18 09:09:53      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:os   io   ar   2014   cti   new   ad   on   

一. 定义Dictionary:

      public static readonly Dictionary<string,object> dictionary = new Dictionary<string,object>();

   #region 单实例

      public static readonly object syncroot = new object();

      public static EditDirectry _editDirectry;

      private EditDirectry()
      { }

      public static EditDirectry GetInstance()
      {
          try
          {
              if (null == _editDirectry)
              {
                  lock (syncroot)
                  {
                      if (null == _editDirectry)
                      {
                          _editDirectry = new EditDirectry();
                      }
                  }
              }
              return _editDirectry;

          }
          catch (Exception ex)
          {
              return null;
          }


      }

#endregion

 

二 .三种方法(增,删,查)

1. 增加

      public bool AddOrUpdateDictionary(string key,object value)
      {
          dictionary.Add(key, value);
          return true;
      }

2.获取

      public T GetDictionary<T>(string key) where T:class
      {
          object value;
          dictionary.TryGetValue(key, out value);
          return value as T;          
      }

3.删除

    public bool RemoveDictionary(string key)
      {
           dictionary.Remove(key);
           return true;
      }

三,调用

  OP op = new OP();
            op.Id = 1;
            op.Name = "Fanfenghua";
            op.Age = 31;

            string Key1 = Guid.NewGuid().ToString("N");

            bool isAdd = EditDirectry.GetInstance().AddOrUpdateDictionary(Key1, op);

            if (isAdd)
            {
                OP op1 = new OP();
                op1 = EditDirectry.GetInstance().GetDictionary<OP>(Key1);
                Console.Write("姓名: " + op1.Name + ",年龄: " + op1.Age);
                Console.Read();
            }

20140818 Dictionary,布布扣,bubuko.com

20140818 Dictionary

标签:os   io   ar   2014   cti   new   ad   on   

原文地址:http://www.cnblogs.com/fanfh/p/3918732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!