在 C# 中可以使用同名的公共 getter 和私有 setter 吗? [英] Is a public getter and a private setter with same name possible in C#?

查看:43
本文介绍了在 C# 中可以使用同名的公共 getter 和私有 setter 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为属性创建公共 getter 和私有 setter?以下正确吗?

How can I create a public getter and a private setter for a property? Is the following correct?

public String Password
{
    set { this._password = value; }
}

private String Password
{
    get { return this._password; }
}

推荐答案

是的,即使使用自动属性也是可能的.我经常使用:

Yes it is possible, even with auto properties. I often use:

public int MyProperty { get; private set; }

这篇关于在 C# 中可以使用同名的公共 getter 和私有 setter 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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