Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private keyword vs no private keyword

Tags:

c#

.net

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?

like image 945
MyNameIsJob Avatar asked Dec 13 '10 15:12

MyNameIsJob


People also ask

Why do we use private keyword?

The private keyword is an access modifier used for attributes, methods and constructors, making them only accessible within the declared class.

What are the differences between the private keyword and private fields in TypeScript?

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 #.

Why should I use private in C#?

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#.

What is private keyword 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.


1 Answers

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.

like image 200
Steve Townsend Avatar answered Sep 19 '22 13:09

Steve Townsend