加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#中的“对象表达”是否存在于F#中?

发布时间:2020-12-15 08:44:00 所属栏目:百科 来源:网络整理
导读: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 exi
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#编程语言?

解决方法

不,那不存在.我认为最接近的是实现这些接口的类.

匿名类型无法符合接口或从任何东西派生.

Lambdas和delegates提供函数作为变量.

Object Expressions的文档声明了这一点,这使它听起来像语言方便.

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.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读