为什么不能在非静态类中声明/实现扩展方法?
我知道 Extension-Method 对于不可实例化的类型是没用的。但是为什么不在可实例化的类中实现呢?这是什么原因?是技术问题还是只是为了更快地找到方法或强制进行更好的软件设计?
最佳答案
You can define a class as static if you want to guarantee that it can't be instantiated, can't derive from or serve as the base for another type, and can contain only static members.
http://msdn.microsoft.com/en-us/library/vstudio/79b3xss3.aspx
让扩展方法在子类中被 edit: static new'd 等将是一个真正的痛苦。
As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded.
静态类在初始化优先级链上更高,使实现更高效。
关于c# - 非静态泛型类中没有扩展方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17818081/