jonnii jonnii - 3 years ago 89
C# Question

Should you use the private access modifier if it's redundant?

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

Without explicit modifier

public class MyClass
{

string name = "james";

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

void SomeMethod() { ... }

}


With explicit modifier

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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download