What is the use of the private keyword if everything, by default, is private?
public class A {
object someObject;
private object someOtherObject;
}
Wouldn't both of the above be private? If they're both private then why the need for the keyword itself?
The private keyword is an access modifier used for attributes, methods and constructors, making them only accessible within the declared class.
TypeScript 3.8 supports the private keyword to declare its members private. And the private fields in TypeScript are the ones newly proposed for JavaScript. These fields are preceded by #.
Variables are stored in memory, so they can be changed whenever you need them. If a variable is private, only the code that declared it could access it. If it's public, anyone can use it. You can use the private keyword to define a variable in C#.
The private keyword is a member access modifier. This page covers private access. The private keyword is also part of the private protected access modifier. Private access is the least permissive access level.
Explicit use of the keyword clarifies the code, whatever the default is.
Avoid guesswork or the need for fact-checking on the part of code reader/maintainer, where possible.
There is a reference for the visibility information via this prior question.
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