c# - "Object Expression"是否像 F# 一样存在于 C# 中?

标签 c# object types f# expression

F#语言中有一个有趣的概念:

对象表达式 (F#)

http://msdn.microsoft.com/en-us/library/dd233237.aspx

定义:

An object expression is an expression that creates a new instance of a dynamically created, anonymous object type that is based on an existing base type, interface, or set of interfaces.

我想知道,C# 中是否存在类似的东西? 或者这些东西只有 F# 编程语言可用?

最佳答案

不,那不存在。我认为最接近的是实现这些接口(interface)的

匿名类型无法符合接口(interface)或派生自任何东西。

Lambda 和委托(delegate)提供函数作为变量。

对象表达式的文档说明了这一点,这听起来像是一种语言便利。

You use object expressions when you want to avoid the extra code and overhead that is required to create a new, named type. If you use object expressions to minimize the number of types created in a program, you can reduce the number of lines of code and prevent the unnecessary proliferation of types. Instead of creating many types just to handle specific situations, you can use an object expression that customizes an existing type or provides an appropriate implementation of an interface for the specific case at hand.

你可以用这样的东西来伪造它(虽然你不能将它限制在一个接口(interface)上)

var foo = new { square = new Func<int, int>(x => x * x) };

关于c# - "Object Expression"是否像 F# 一样存在于 C# 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20703002/

相关文章:

c# - 为什么这种泛型场景会导致 TypeLoadException?

powershell - 如何向自定义对象添加更多属性值

jquery - 指令 AngularJS 仅输入 1 到 10 范围内的数字,jquery 指令 AngularJS

javascript - 如何在javascript中组合函数名称?

javascript - 对象作为对象属性名称?

haskell - 代理、类型级别符号和 JSON

Haskell unsafe将新类型强制转换为相同的新类型

c# - 为什么在 WCF 应用程序的 Web.config 文件中添加 httpRuntime targetFramework 可以解决与 TLS 相关的连接问题?

c# - Roles.Enabled 为 true,即使 roleManger.enabled 设置为 false

c# - "Unable to read data from the transport connection: net_io_connectionclosed."- Windows Vista 商业版和 SMTP