自动实现的属性 [英] auto-implemented properties

查看:63
本文介绍了自动实现的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有C#中的自动实现属性是什么.
如何以及何时使用它?

What is auto-implemented properties in all C# .
How and when to use it??
Is that specific to visual studio version.

推荐答案

自动实现的属性只是普通属性的语法快捷方式.以下自动实现的属性:

Auto-implemented properties are only a syntactic shortcut to ordinary properties. The following auto-implemented property:

public bool MyProperty { get; set; }



...完全一样:



...is exactly the same like:

private bool myField;
public bool MyProperty {
  get { return myField; }
  set { myField = value; }
}



如果使用自动实现的属性语法,则编译器将生成后备字段和getter/setter.如果手动实现该属性,则必须编写更多代码,但可以更好地控制getter/setter中的代码.



If you use the auto-implemented property syntax, the compiler will generate the backing field and getter/setter. If you implement the property manually, you have to write more code but you have more control over the code inside getter/setter.



这篇关于自动实现的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆