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?
Private: The private access modifier is specified using the keyword private. The methods or data members declared as private are accessible only within the class in which they are declared. Any other class of the same package will not be able to access these members.
The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected.
Therefore the private access modifier is not allowed for classes.
I think explicity stating private helps in readability. It won't allow for a programmer to interpret its visibility differently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With