如果它是冗余的,你应该使用私有访问修饰符? [英] Should you use the private access modifier if it's redundant?

查看:186
本文介绍了如果它是冗余的,你应该使用私有访问修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class MyClass
{

    string name = "james";

    public string Name {
        get { return name; }
        set { name = value; }
    }

    void SomeMethod() { ... }

}

使用显式修饰符

public class MyClass
{

    private string name = "james";

    public string Name {
        get { return name; }
        set { name = value; }
    }

    private void SomeMethod() { ... }

}






我一直使用后者,但最近我开始采用前一种风格。 private是多余的,因为这是默认的访问器修饰符,所以排除它是没有意义的。


I've always used the latter, but recently I've started adopting the former style. The private is redundant as that's the default accessor modifier, so doesn't it make sense to exclude it?

推荐答案

说明私人有助于可读性。它不允许程序员以不同的方式解释它的可见性。

I think explicity stating private helps in readability. It won't allow for a programmer to interpret its visibility differently.

这篇关于如果它是冗余的,你应该使用私有访问修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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