如何为物业制作公共吸气剂和私人定位器?以下是否正确?
public String Password
{
set { this._password = value; }
}
private String Password
{
get { return this._password; }
}
Run Code Online (Sandbox Code Playgroud)
And*_*bel 89
是的,即使使用自动属性也是可能的.我经常使用:
public int MyProperty { get; private set; }
Run Code Online (Sandbox Code Playgroud)
Cod*_*ray 11
是的,从C#2.0开始,您可以为getter和属性的setter指定不同的访问级别.
但是您的语法错误:您应该将它们声明为同一属性的一部分.只需标记要限制的那个private.例如:
public String Password
{
private get { return this._password; }
set { this._password = value; }
}
Run Code Online (Sandbox Code Playgroud)
public String Password
{
private set { this._password = value; }
get { return this._password; }
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用自动实现的属性:
public String Password { get; private set; }
Run Code Online (Sandbox Code Playgroud)