2

I've read this very well written blog post by Eric Lippert, but it doesn't explain the specific case of accessibility modifiers.

Conventional stackoverflow wisdom says to always specify "private" explicitly, which would imply that members should have required the modifier.

Community
  • 1
  • 1
Jon
  • 5,275
  • 5
  • 39
  • 51
  • because MS is cool like that :) – Tom May 17 '12 at 05:03
  • 1
    The C# language specification says this: "When a class-member-declaration does not include any access modifiers, private is assumed". For the sake of obviousness, I still usually explicitly type out "private," though I know some people are against that convention. – Cᴏʀʏ May 17 '12 at 05:07
  • http://www.cookcomputing.com/blog/archives/private-access-modifier-in-csharp – Michael Buen May 17 '12 at 05:08
  • 3
    The question is not "What are the default modifiers" or "Why should I add it explicitly". There's certainly a lot of debate about whether to explicitly add the modifier or to use the inferred one, but I don't see why that has translated into downvotes for this question, which is simply asking WHY they're optional. – Brook May 17 '12 at 12:40

3 Answers3

1

Well It's not like there's no default modifiers set if not defined. In C# a default modifier is set even if you don't specify so at times you might want to know your default modifiers for your program to execute your way (intended result).

An enum, struct, or class will default to internal under typical conditions (in a namespace or just sitting there alone in a file), but will default to private when declared inside a struct or class.

All methods, fields, and properties has default access modifier as "Private".

Gabe
  • 84,912
  • 12
  • 139
  • 238
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
  • Slightly more detailed answer over here: http://stackoverflow.com/questions/3763612/default-visibility-for-c-sharp-classes-and-members-fields-methods-etc – Cᴏʀʏ May 17 '12 at 05:09
  • Why do you think that enums are public by default? My reading of the C# spec says they're public when in a namespace or compilation unit, and private when in a class or struct. – Gabe May 17 '12 at 05:23
  • I'm asking why methods, fields, and properties have a default access modifier instead of requiring the programmer to specify it. – Jon May 17 '12 at 05:43
  • @gabe ..yes in class declarations it defaults to private – Abhishek Singh May 17 '12 at 07:39
  • Oops, I meant they are internal when in a namespace or compilation unit. – Gabe May 17 '12 at 12:14
  • 2
    This doesn't even answer the question, which was "why are they optional"? The OP didn't ask what the defaults are. – Brook May 17 '12 at 12:40
  • @Brook .. i get it ..but i think i answered that in comment and then one might have to ask himself a question "Why int has 4 Bytes in c?" – Abhishek Singh May 17 '12 at 12:44
  • @gabe enum defaults to public outside class – Abhishek Singh May 17 '12 at 12:48
  • 1
    @roronoazorro: We know what the defaults are, we know they are optional. You've restated the premise of the question, but not answered it. – Brook May 17 '12 at 12:49
  • @brook ..you read my previous comment..just get me the answer y int has 4 bytes?..if you go in that direction the question must have been closed.. – Abhishek Singh May 17 '12 at 12:51
  • @roronoazorro: No, `enum` defaults to `internal` outside of a class or struct. You can try it yourself. If you create an `enum` with no access modifiers in one assembly you will see that you cannot access it from another one. – Gabe May 18 '12 at 11:14
0

Possibly the designers of C# wanted and liked "religious" fights on whether or not to include refundant access modifiers?

(In cases where only one access level is possible, access modifiers are usually disallowed: For example, a member of an interface or an enum is always public but it's not allowed to write that. A partial method is always private but you can't write that. Since a static constructor can't be called explicitly, there's no meaningful access level. An explicit interface implementation is not really visible for direct call, but is freely accessible through the interface type (if the interface type is visible from the outside). Namespaces can have no access modifier.)

The opposite rule from what you suggest, would of course be a rule saying that it is not allowed to specify a modifier that doesn't change anything. That would mean that a direct member of a namespace (including the implicit "global namespace") could not be explicitly declared internal, and that a member of a class or struct (including nested types) could not have the keyword private. Some people use this convention.

Note that getters and setters of properties and indexers cannot specify a redundant access level. The only thing allowed here is for just one of the two accessors (there must be two in that case) to specify a more strict access.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
0

As is usually the case with this type of question, a definitive answer could only come from those who created the language (assuming it is not addressed in the documentation, of course).

But it's probably safe to assume that it is because, in the words of David Naugler, in addition to being a C-based language, "C# has been strongly influenced by Java [and] C++ and is best viewed as a descendant of both(...)."

As a side note, Anders Hejlsberg, the original author of Turbo Pascal and the chief architect of Delphi, is also the lead architect behind the creation of C#.

I realise this is sort of dodging the question, so you may now go ask why does C++ originally allow for optional visibility modifiers =p

Marc.2377
  • 7,807
  • 7
  • 51
  • 95