c# - 非静态泛型类中没有扩展方法?

标签 c# .net extension-methods static-classes

为什么不能在非静态类中声明/实现扩展方法?

我知道 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/

相关文章:

c# - 如何为类型创建扩展方法

Swift where 条件检查属性是否已实现

c# - Ngen 和 .net-native 一次为多少种 x86 处理器创建 native 代码?

c# - 如何使用用户的默认电子邮件客户端弹出撰写/创建邮件对话框?

c# - 程序卸载时调用函数/Api

.net - 邮件发送失败。无法将数据写入传输连接

linq - 扩展方法 : how to make it work with GUID also

c# - 下载 mp3 并另存为铃声

c# - 可重复使用的 ModalPopupExtender

c# - 将列表值拆分为逗号分隔的字符串