//Extended dictionary
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
public TValue this[TKey key]
{
get
{
TValue val;
if(base.TryGetValue(key, out val))
{
return val;
}
else
{
throw new KeyNotFoundException(string.Format("The given key ({0}) was not present in the dictionary.", key));
}
}
set
{
base[key] = value;
}
}
}
public class Program
{
public static void Main(string[] args)
{
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"a", "b"}
//};
MyDictionary<string, string> dic = new MyDictionary<string, string>()
{
{"a", "b"}
};
Console.WriteLine(dic["aa"]);
}
}
}
from : http://rextester.com/YKWBI65251
沒有留言:
張貼留言