I create a sample class in vs2010.
Through Class View, I see the default access modifier for Main is internal.
I also see some people say that the default access modifier for Main is "implicitly private".
Visual Studio 2010 automatically defines a program’s Main() method as implicitly private. Doing so ensures other applications cannot directly invoke the entry point of another.
I know there are differences between internal and private. So which one is correct?
If your code appears like this:
then that's a private method. (The
static
part is orthogonal to accessibility, but is necessary to be an entry point.) In general, the default accessibility of any member is the most private accessibility that you could declare it. So for methods in a class or a struct, that's private. For top-level (non-nested) types it's internal. For any member declared in a class/struct, it's private1. For interface and enum members, it's public.It's hard to understand exactly what you're seeing via Class View without seeing either your code or a screenshot of Class View, but the default accessibility for a method is definitely private. That's true regardless of whether it's the
Main
method or not.1 Explicit interface implementation is a bit odd here, as it's neither private nor public; it's simply not accessible through the type, only through the interface.
You can't see the default access modifier for a member in the class browser, you can see the actual access modifier.
The default access modifiers for classes at the namespace level is
internal
, whereas the default access modifier for class members (including nested classes) isprivate
. There's no special case for theMain()
function. If there's no access modifier before it (a la Jon Skeet's example), then it'sprivate
. If there is one, then that's what it is.By Default the access specifier for Main() in C# is private.
This is what I got when I saw the MSIL(IL) code in ILDASM.
You can see that Main() is private.
Both, the default class modifier is internal. The main method is a method and is private. In general, classes without a modifier are internal, class-members (such as methods and fields) without a declaration are private.
Private members are accessible only within the body of the class in which they are declared.
Internal types or members are accessible only within files in the same assembly
Internal 'is like' public but only for all elements of the same assembly. Class1 of assembly1 cannot 'see' or access any internal element of assembly2.
Although you tagged your question c#, let me say that the access modifiers for the default
Program.Main
generated by VS2010 actually depends on the project template, on these differ for each language. I quickly tried the following:In a VB.NET console project, the
Program
module (static class) isFriend
(i.e.internal
in C#) and theMain
static method isPublic
.In a C# console project,
Program
isinternal
, andMain
isprivate
.That is, a C# project will simply use the default access modifiers (
internal
for classes,private
for methods).