c# - C#3 和 4 中扩展方法的区别

标签 c# c#-4.0 extension-methods versions

我想我记得很久以前读过,在 C#3 中,扩展方法只能应用于基本类型和接口(interface);并且在 C#4 中,它们可用于扩展任何类型。这与我现在看到的不相符,我发现很难找到它的记录。

这是真的还是我的内存造成的?

关于哪些类型可以扩展的规则是什么?

C# 3 和 4 之间有什么区别吗?

最佳答案

Is there any truth to this?

没有。

What are the rules relating to which types can be extended?

调用规则在 C# 4 规范的第 7.6.5.2 节中。声明规则在第 10.6.9 节。

回答您的具体问题:非托管指针类型可能无法扩展。

Are there any differences between C# 3 and 4?

是的。 C# 4 添加了处理“动态”的额外规则。扩展方法动态解析;如果你有

dynamic d = 10;
d.MyIntExtension();

那么动态语言运行时将不会在 int 上找到扩展方法。见

Will the dynamic keyword in C#4 support extension methods?

了解更多详情。

关于c# - C#3 和 4 中扩展方法的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4593230/

相关文章:

.net - : . Any() 与 .Count() > 0 哪种方法性能更好?

c# - 在扩展方法中创建的实例为空

entity-framework - 将 EF 查询与 BL 分离 - 扩展方法 VS Class-Per-Query

c# - log4net在运行时删除文件是否可能c#

mysql - 使用 Entity Framework 获取唯一行

c#-4.0 - 真正的随机C#生成器

asp.net-mvc - 如何在不终止用户 session 的情况下部署 MVC 4 应用程序

c# - 动态转换和消息类型

c# - ASP.NET Active Directory C# 字段规范

c# - 如何对句点进行 URL 编码?