简体   繁体   English

有没有办法在C#中获得F#的构造函数 - 参数 - 自动不可变 - 私有成员功能?

[英]Is there a way to get F#'s constructor-parameters-are-automatically-immutable-private-members feature in C#?

In F#, the constructor parameters are automatically saved as immutable fields in the class without having to write the declaration of the field or the copying of the constructor parameter to the field in the constructor. 在F#中,构造函数参数自动保存为类中的不可变字段,而不必编写字段声明或将构造函数参数复制到构造函数中的字段。

Is there any way to mimic this functionality in C#? 有没有办法在C#中模仿这个功能? Is there a reason why a future version of C# couldn't have this feature (ie is there some fundamental limitation of the existing language that conflicts with this feature)? 是否存在未来版本的C#无法具备此功能的原因(即现有语言是否存在与此功能冲突的某些基本限制)?

The C# equivalent will be records . C#等价物将是记录 These are currently earmarked for C# 8, but there's no guarantees (especially as they were proposed for C# 6 & 7 too and were delayed in both cases). 目前这些都是专门用于C#8的,但是没有任何保证(特别是因为它们也被提议用于C#6和7,并且在两种情况下都被推迟)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM