Is there an IDictionary implementation that, on missing key, returns the default value instead of throwing?

前端 未结 15 1839
暗喜
暗喜 2020-11-27 04:00

The indexer into Dictionary throws an exception if the key is missing. Is there an implementation of IDictionary that instead will return de

15条回答
  •  暖寄归人
    2020-11-27 04:43

    If you are using ASP.NET MVC, you could leverage the RouteValueDictionary class that do the job.

    public object this[string key]
    {
      get
      {
        object obj;
        this.TryGetValue(key, out obj);
        return obj;
      }
      set
      {
        this._dictionary[key] = value;
      }
    }
    

提交回复
热议问题