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?
-
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 isprivate. 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;}leppie : 4 seconds!!! But structs are private too, same rules.Marc Gravell : Where did I get that from then? Must have gone mad...leppie : Hehehe, I have heard that rumour before, and even just double checked in case I was going insane :)Marc Gravell : 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 ;-pleppie : Popquiz: When can a private member be accessed directly from another class?Jon Skeet : @leppie: Jura gur "bgure glcr" vf n arfgrq glcr bs gur bevtvany pynff. (ROT13)Marc Gravell : answered earlier, but ROT13d for fairness: Jura gur "bgure pynff" vf arfgrq jvguva gur pynff jvgu gur cevingr zrzore.leppie : Sorry marc, Jon's answer is correct. VG PNA OR N ARFGRQ-ARFGRQ-ARFGRQ PYNFF. :) -
Yes, all type members are private. But, no, that cant be changed.
-
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.
Vince : It wouldn't be confusing if It was easy to do.Jon Skeet : @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.Marc Gravell : Agree; it would be confusing to me... -
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,
publickeyword would be prefixed to your newly added class.see here to edit visual studio templates
0 comments:
Post a Comment