C#:getter/setter

发布于 2024-11-24 03:04:10 字数 143 浏览 3 评论 0原文

我在某处看到类似以下内容,并想知道这是什么意思。我知道它们是 getter 和 setter,但想知道为什么字符串 Type 是这样定义的。谢谢你帮助我。

public string Type { get; set; }

I saw something like the following somewhere, and was wondering what it meant. I know they are getters and setters, but want to know why the string Type is defined like this. Thanks for helping me.

public string Type { get; set; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

温馨耳语 2024-12-01 03:04:10

这些是自动实现的属性(简称“自动属性”)。

编译器将自动生成以下简单实现的等效项:

private string _type;

public string Type
{
    get { return _type; }
    set { _type = value; }
}

Those are Auto-Implemented Properties (Auto Properties for short).

The compiler will auto-generate the equivalent of the following simple implementation:

private string _type;

public string Type
{
    get { return _type; }
    set { _type = value; }
}
杀お生予夺 2024-12-01 03:04:10

这是一个自动属性,它是其简写符号:

private string type;
public string Type
{
  get { return this.type; }
  set { this.type = value; }
}

That is an auto-property and it is the shorthand notation for this:

private string type;
public string Type
{
  get { return this.type; }
  set { this.type = value; }
}
生死何惧 2024-12-01 03:04:10

在 C# 6 中:

现在可以将自动属性声明为字段:

public string FirstName { get; set; } = "Ropert";

只读自动属性

public string FirstName { get;} = "Ropert";

In C# 6:

It is now possible to declare the auto-properties just as a field:

public string FirstName { get; set; } = "Ropert";

Read-Only Auto-Properties

public string FirstName { get;} = "Ropert";
冷清清 2024-12-01 03:04:10
public string Type { get; set; } 

与做没有什么不同

private string _Type;

public string Type
{    
  get { return _Type; }
  set { _Type = value; }
}
public string Type { get; set; } 

is no different than doing

private string _Type;

public string Type
{    
  get { return _Type; }
  set { _Type = value; }
}
夜夜流光相皎洁 2024-12-01 03:04:10

这意味着编译器在运行时定义了一个支持字段。这是自动实现属性的语法。

详细信息: 自动-实现的属性

This means that the compiler defines a backing field at runtime. This is the syntax for auto-implemented properties.

More Information: Auto-Implemented Properties

深居我梦 2024-12-01 03:04:10

您还可以使用 lambda 表达式

public string Type
{
    get => _type;
    set => _type = value;
}

You can also use a lambda expression

public string Type
{
    get => _type;
    set => _type = value;
}
偏闹i 2024-12-01 03:04:10

它是一个自动支持的属性,基本上相当于:

private string type;
public string Type
{
   get{ return type; }
   set{ type = value; }
}

It's an automatically backed property, basically equivalent to:

private string type;
public string Type
{
   get{ return type; }
   set{ type = value; }
}
暮倦 2024-12-01 03:04:10

这些称为自动属性。

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

在功能上(以及就已编译的 IL 而言),它们与具有支持字段的属性相同。

These are called auto properties.

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

Functionally (and in terms of the compiled IL), they are the same as properties with backing fields.

薆情海 2024-12-01 03:04:10

随着 C# 6 的发布,您现在可以对私有属性执行类似的操作。

public constructor()
{
   myProp = "some value";
}

public string myProp { get; }

With the release of C# 6, you can now do something like this for private properties.

public constructor()
{
   myProp = "some value";
}

public string myProp { get; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文