views:

190

answers:

4

Hello,

Is there a way to make the default access modifier public for variable/method/class declarations?

I think by default, class declarations are private yes?

+5  A: 

You can't change the defaults. They default to the most restrictive.

The default accessibility (for the type) for a top-level type is internal. The default accessibility (for the type) for a nested type is private. The default accessibility for members is private.

The only time it isn't the most restrictive is for the explicit part of an automatically implemented property, where you can make it more restrictive by adding a modifier:

public int Foo {get;private set;}
Marc Gravell
4 seconds!!! But structs are private too, same rules.
leppie
Where did I get that from then? Must have gone mad...
Marc Gravell
Hehehe, I have heard that rumour before, and even just double checked in case I was going insane :)
leppie
The thing is, I remember reading it recently and thinking: "huh? is that right" and actually trying it! Guess I borked the test... never mind; I added some extra flavor, too ;-p
Marc Gravell
Popquiz: When can a private member be accessed directly from another class?
leppie
@leppie: Jura gur "bgure glcr" vf n arfgrq glcr bs gur bevtvany pynff. (ROT13)
Jon Skeet
answered earlier, but ROT13d for fairness: Jura gur "bgure pynff" vf arfgrq jvguva gur pynff jvgu gur cevingr zrzore.
Marc Gravell
Sorry marc, Jon's answer is correct. VG PNA OR N ARFGRQ-ARFGRQ-ARFGRQ PYNFF. :)
leppie
+2  A: 

Yes, all type members are private. But, no, that cant be changed.

leppie
+3  A: 

The general rule is that the default is the most private access level which you could specify. The only slight variation on this is when you make one part of a property (usually the setter) more private than the rest of the property.

Being able to change the default would be extraordinarily confusing to people maintaining your code. There are many who argue that you should never use the defaults anyway always explicitly specifying the visibility.

Jon Skeet
It wouldn't be confusing if It was easy to do.
Vince
@Vince: Yes it would - because you couldn't look at a member and immediately know what the visibility was. You'd have to look elsewhere to find out what the defaults had been set to. Not a good idea, IMO.
Jon Skeet
Agree; it would be confusing to me...
Marc Gravell
+1  A: 

You cannot change the behavior as rightly indicated by other answers

But you can edit your class template file, so that each time you add a class from your solution explorer, public keyword would be prefixed to your newly added class.

see here to edit visual studio templates

AB Kolan