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

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

问题描述

由于这两个例子是等价的,其中你认为是preferrable?

Given that these two examples are equivalent, which do you think is preferrable?

如果不明确修改

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() { ... }

}


我一直使用后者,但最近我已经开始采用前者的风格。私人是多余的,因为这是默认的访问修饰符,因此不会有意义排除呢?

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天全站免登陆