扩展方法

    static class TestExtend
    {
        /// 
        /// 判断是否有重复添加
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void AddSingle(this Dictionary dict, Tkey key, TValue value)
        {
            if (!dict.ContainsKey(key))
            {
                dict.Add(key, value);
            }
        }

        /// 
        /// 批量添加
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void AddRange(this Dictionary dict, IEnumerable> keyValues)
        {
            foreach (var item in keyValues)
            {
                if (!dict.ContainsKey(item.Key))
                    dict[item.Key] = item.Value;
            }
        }

        /// 
        /// 替换为指定的字符
        /// 
        /// 
        /// 
        /// 
        public static string ReplaceAToChar(this string str, string toChar)
        {
            return str.Replace("a", toChar);
        }

        /// 
        /// 判断是否为数字
        /// 
        /// 
        /// 
        public static bool isNumerice(this string str)
        {
            Regex r = new Regex(@"^\d+(\.)?\d*$");
            return (!r.IsMatch(str)) ? false : true;
        }
    }
        static void Main(string[] args)
        {
            var model = new Dictionary<string, string>();
            model.Add("001", "中国");
            model.AddSingle("002", "美国");//判断重复添加值

            var listModel = new Dictionary<string, string>();
            listModel.AddRange(model);//批量添加Key/Value值
            foreach (var item in listModel)
            {
                Console.WriteLine("Key:" + item.Key + "  Value:" + item.Value);
            }

            string test = "jacky";
            Console.WriteLine(test.ReplaceAToChar("@"));
            test = "12.88";
            Console.WriteLine(test.isNumerice());
        }

输出值:

转载于:https://www.cnblogs.com/myjacky/p/3422423.html

你可能感兴趣的:(扩展方法)