54

Ok this is bugging me.. I know I've read it somewhere and google isn't helping.

What is the accessibility level of a method that does not specify an access modifier?

void Foo()
{
    //code
} 

I want to say internal but I'm not 100% sure.

Steve Guidi
  • 19,700
  • 9
  • 74
  • 90
Leroy Jenkins
  • 2,680
  • 6
  • 25
  • 33

8 Answers8

60

The default accessibility for a type is internal, but the default accesibility of that type's members depends on the type.

Generally speaking, members of a class are private by default, where as members of a struct are public by default. This varies by language; default struct access modifiers for C++ are public, where as for C#, they are private.

Steve Guidi
  • 19,700
  • 9
  • 74
  • 90
  • 7
    Are you guys sure about this downvote? If I place a method in a C# class, with no access modifier, the C# 3.0 compiler builds it as a private method. – Steve Guidi May 26 '10 at 23:19
  • 1
    Steve is correct. The default accessibility for class methods is `private`. Try it for yourself. – Michael Petrotta May 26 '10 at 23:28
  • 1
    This answer is correct. Just verified it in VS2010 because I originally thought it was wrong too. – Donnie May 26 '10 at 23:33
  • 3
    Members of a `struct` are `public` by default in C++, but according to the MSDN page linked from several other answers, members of a `struct` are `private` by default in C#. – Daniel Pryden May 26 '10 at 23:34
  • 1
    -1 because two authoritative references say members of a `struct` are `private` by default in C#. (MSDN is one, and *The C# Programming Language, 3rd Ed.* is the other.) – Daniel Pryden May 26 '10 at 23:50
  • 1
    Default accessibility for member variables either for struct or class is private by default if not mentioned. Here is the MSDN link supporting the same. It is evident in second paragraph of "Class and Struct accessibility" section : https://msdn.microsoft.com/en-us/library/ms173121.aspx – RBT Jan 28 '15 at 08:25
23

Assuming this is a C# method, since you have the ".net" tag.

People need to differentiate between "member" accessibility and "class" accessibility.

Sachin Joseph
  • 18,928
  • 4
  • 42
  • 62
Minyu
  • 454
  • 1
  • 3
  • 10
21

Yes, internal is the default for classes, but private is the default for members.

EMP
  • 59,148
  • 53
  • 164
  • 220
12

For a class: Internal is the default if no access modifier is specified.

For a method: Private is the default if no access modifier is specified.

Secko
  • 7,664
  • 5
  • 31
  • 37
8

From The C# Programming Language, Third Edition by Anders Hejlsberg et al, section 10.3.5 ("Class Members - Access Modifiers") on page 434:

A class-member-declaration can have any one of the five possible kinds of declared accessibility (§3.5.1): public, protected internal, protected, internal, or private. Except for the protected internal combination, it is a compile-time error to specify more than one access modifier. When a class-member-declaration does not include any access modifiers, private is assumed. [Emphasis mine]

And then in section 11.2 ("Struct Members") on page 539:

Except for the differences noted in §11.3, the descriptions of class members provided in §10.3 through §10.14 apply to struct members as well.

Section 11.3 does not mention anything about access modifiers, so my reading of this implies that members of a struct without an access modifier are also private by default. This corresponds with what MSDN says and with my own experience.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
5

Oh wait, there's one more thing ....

interface method declarations are of course public by definition. So the following implementation is public, without an explicit access modifier.

public class MyClass : IEqualityComparer<MyClass>
    bool IEqualityComparer<MyClass>.Equals(MyClass x , MyClass y) {}
}
radarbob
  • 4,964
  • 2
  • 23
  • 36
  • btw interface methods cannot be private – Dragon Nov 28 '15 at 09:09
  • oh, yeah... [Inheriting `EqualityComparer` is recommended, not implementing the interface](https://msdn.microsoft.com/en-us/library/ms132123%28v=vs.110%29.aspx). see "Remarks" – radarbob Nov 28 '15 at 22:18
0

Class methods are private and sealed by default in .NET. This means the method is only visible within the class and cannot be overridden by the inherited class.

HaohaoLin
  • 91
  • 2
  • 7
0

hope this clarifies all as per screenshot directly from MSDN

enter image description here

pixel
  • 9,653
  • 16
  • 82
  • 149