在C# 3.5中引入了一种新的特性:扩展方法.其实质只是调用一个静态方法,因此很多人认为它只是一个语法糖.但是, 我觉得由于扩展方法能够在不破坏现有的模块的情况下对其进行扩展,结合c#中其他的一些新特性,能够实现很多神奇的效果,是我们能够写出更加面向对象的程序,大大优化代码结构. 例如对Dictionary的使用,我们可能常常会需要写出类似于以下的代码: ?View Code CSHARP Dictionary<string, SomeClass> dictionary = new Dictionary<string, SomeClass>(); string key = "some key"; SomeClass c; if (dictionary.ContainsKey(key)) { c = dictionary[key]; } else { c = new SomeClass(); dictionary.Add(key, c); } //do something with …
转载于:https://www.cnblogs.com/kunzhu/archive/2011/03/02/2279854.html