在C#中定义私有属性,例如Typescript

Bee*_*ice 4 c# typescript asp.net-core

C#中是否有语法允许我定义私有属性,例如Typescript?

示例:打字稿:

constructor (private field1: string) { }
Run Code Online (Sandbox Code Playgroud)

在C#中,我必须这样做:

private readonly string field1;
public MyClass(string field1)
{
    this.field1 = field1;
}
Run Code Online (Sandbox Code Playgroud)

更新1:

我正在寻找AspNet核心依赖项注入的语法糖。

j4n*_*4nw 5

C#7中没有为此提供语法的糖,您必须编写样板。

听起来像主要的构造函数就是您要寻找的东西-它们在C#6 beta中,但最终放弃了。

http://www.alteridem.net/2014/09/08/c-6-0-primary-constructors/

  • 看起来主要的构造函数将以C#8中的[records](https://github.com/dotnet/csharplang/blob/master/proposals/records.md)形式返回。 (2认同)