[英]Assign values to auto-implemented properties
How can i assign values to auto implemented backing fields like this:: 我如何为自动实现的后备字段分配值,如下所示:
`private string code = "N.A";
private string name = "not known";
private int age = 0;
// Declare a Code property of type string:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}`
how would i accomplish the same thing using auto properties for example: 我将如何使用自动属性来完成同一件事,例如:
public string Code { get; set; }
Obviously you cant just add the value directly to the auto property like this: 显然,您不能像这样直接将值直接添加到auto属性中:
public string Code = "N.A" { get; set; }
The thing about auto-implemented properties is exactly that they don't have a backing field (available to you). 关于自动实现属性的事情恰恰是它们没有后备字段(您可以使用)。
There are 2.5 solutions to the problem 有2.5个解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.