C# Default access modifier of Main() method - c#

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:
static void Main()
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.

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) is Friend (i.e. internal in C#) and the Main static method is Public.
In a C# console project, Program is internal, and Main is private.
That is, a C# project will simply use the default access modifiers (internal for classes, private for methods).

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) is private. There's no special case for the Main() function. If there's no access modifier before it (a la Jon Skeet's example), then it's private. If there is one, then that's what it is.

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.

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.

Related

make access modifier one for all members in class

Have defined such class:
class Data
{
internal string f_source;
internal string f_output;
internal string password;
}
As you see, I'm defining the access modifier for each member in it, explicitly.
Does exist some way to define the default access modifier for the all members in class at once?
Maybe, there is some attribute that makes such dream come true... Don't know...
I've tried to use the access modifier before the class declaration:
internal class Data
{
string f_source;
string f_output;
string password;
}
But, no success!
Are the any suggestions: "how to fix such a problem?"
By design what your are asking for is not possible in C#.
The specifications of C# specifies which access modifier to use if none is given. The default is the most restricted access modifier that's legal. That is a type directly in a namespace (Ie not a nested type) is internal whereas any member is private unless otherwise specified.
This is, I'm sure, a design decision partly based on experience with how access modfiers in C++ works. a series of bugs can come from have specific sections of a file declaring private, publicetc. It's a lot more expressive to have to state it (or know that if not specified it's as restricted as possible). Keeping the restricted as the default fits well with keeping as much as possible as internal (*) implementation details and only exposing what you really need to expose
(*) not the modifier
Your second definition type makes your class accessable only within same assembly just for that class. No way to make all class members as internal for all members.
Chek this: msdn internal defination&example
Good Luck!

Should I set every WinForm object, such as textbox or button, public? What are the risks?

I have noticed for high demand in my projects for objects such as textboxes or buttons public.
Is there any problems by setting them public?
What does public, private, static really mean?
Access Modifiers (C# Programming Guide)
public
The type or member can be accessed by any other code in the same
assembly or another assembly that references it.
private
The type or member can be accessed only by code in the same class or
struct.
protected
The type or member can be accessed only by code in the same class or
struct, or in a class that is derived from that class.
internal
The type or member can be accessed by any code in the same assembly,
but not from another assembly.
protected internal
The type or member can be accessed by any code in the assembly in
which it is declared, or from within a derived class in another
assembly. Access from another assembly must take place within a class
declaration that derives from the class in which the protected
internal element is declared, and it must take place through an
instance of the derived class type.
There is no security risk as far as I know. But there may be better alternative approach to design your program
public, private, etc are called access modifiers and determine the rules for which other code are allowed to access each member.
There is no technical problem of setting controls as public. But I would not recommend it. Having everything public is a good recipe for creating spaghetti code.
Keep all access to controls within your form and expose only a small set of public methods with a simple interface for external actors to access data and operations on the form.
You should not, for a clean design.
You should in reality put the logic of your application outside forms!
However, if you want to keep logic inside forms, you should at least expose them with public properties and methods, without giving direct access to form controls.
For example you can provide things like a method "EnableSave" or "QuitApplication" or "UpdateState".
Public, private and static deal with scope and what can talk to the objects / methods
Public -> Other classes can create an instance of your class (assuming the class is public) and call this object / method directly
Private -> Other classes can create an instance of your class (assuming the class is public) but can NOT access this object / method
Static -> Other classes can directly access this object / method (assuming class is public and method is public static) such as: YourClassName.ObjectOrMethod without having to create an instance of YourClassName
The best access modifier to give to a gui component when you want to access it directly is :internal (that is the default in VB.NET for example).
However you shouldn't give a public or internal modifier on a GUI control and you shouldn't access it directly, because the presentation layer and business logic layer should be kept separated in a well designed architecture ...

private class outside namespace

I want to create a class outside a namespace so that its default access modifier is 'PRIVATE'. I am doing like this:
namespace KnowStructs
{
class Clas1 {}
}
class MyClass {}
But still my class 'MyClass' is referred as Internal when I look in Reflector.
Can anyone help me out.
From Accessibility Levels:
Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.
and:
Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.
and for private:
Private members are accessible only within the body of the class or the struct in which they are declared
That is, the private keyword is explicitly defined in terms of a containing class or struct.
So whatever you're trying to do, I don't understand it. How could a top level private type possibly be useful? No other code would be able to reference it (in any way, even if it had e.g. static factory methods).
If a private class is allowed that is not a nested type then what would that mean? If it is more restrictive than internal then how would you use it or create an instance. Any use case will require it to be internal at a minimum. I would like to see how you intend to use it.
It simply makes no logical sense.
Whereas having a private nested class scopes itself to the parent containing class. If it were internal then you still will be able to make an instance within the assembly.
So for classes having no modifier is internal by default for non nested types and private for nested types as .Net always applies the most restrictive access when no modifier is specified.
You can make the class internal, if you only want to be accessible by classes in your namespace

Internal vs. Private Access Modifiers

What is the difference between the internal and private access modifiers in C#?
internal is for assembly scope (i.e. only accessible from code in the same .exe or .dll)
private is for class scope (i.e. accessible only from code in the same class).
Find an explanation below. You can check this link for more details -
http://www.dotnetbull.com/2013/10/public-protected-private-internal-access-modifier-in-c.html
Private: - Private members are only accessible within the own type (Own class).
Internal: - Internal member are accessible only within the assembly by inheritance (its derived type) or by instance of class.
Reference :
dotnetbull - what is access modifier in c#
internal members are visible to all code in the assembly they are declared in.
(And to other assemblies referenced using the [InternalsVisibleTo] attribute)
private members are visible only to the declaring class. (including nested classes)
An outer (non-nested) class cannot be declared private, as there is no containing scope to make it private to.
To answer the question you forgot to ask, protected members are like private members, but are also visible in all classes that inherit the declaring type. (But only on an expression of at least the type of the current class)
Private members are accessible only within the body of the class or the struct in which they are declared.
Internal types or members are accessible only within files in the same assembly
private - encapsulations in class/scope/struct ect'.
internal - encapsulation in assemblies.
internal members are accessible within the assembly (only accessible in the same project)
private members are accessible within the same class
Example for Beginners
There are 2 projects in a solution (Project1, Project2) and Project1 has a reference to Project2.
Public method written in Project2 will be accessible in Project2 and the Project1
Internal method written in Project2 will be accessible in Project2 only but not in Project1
private method written in class1 of Project2 will only be accessible to the same class. It will neither be accessible in other classes of Project 2 not in Project 1.
Internal will allow you to reference, say, a Data Access static class (for thread safety) between multiple business logic classes, while not subscribing them to inherit that class/trip over each other in connection pools, and to ultimately avoid allowing a DAL class to promote access at the public level. This has countless backings in design and best practices.
Entity Framework makes good use of this type of access

What does the "private" modifier do?

Considering "private" is the default access modifier for class Members, why is the keyword even needed?
There's a certain amount of misinformation here:
"The default access modifier is not private but internal"
Well, that depends on what you're talking about. For members of a type, it's private. For top-level types themselves, it's internal.
"Private is only the default for methods on a type"
No, it's the default for all members of a type - properties, events, fields, operators, constructors, methods, nested types and anything else I've forgotten.
"Actually, if the class or struct is not declared with an access modifier it defaults to internal"
Only for top-level types. For nested types, it's private.
Other than for restricting property access for one part but not the other, the default is basically always "as restrictive as can be."
Personally, I dither on the issue of whether to be explicit. The "pro" for using the default is that it highlights anywhere that you're making something more visible than the most restrictive level. The "pro" for explicitly specifying it is that it's more obvious to those who don't know the above rule, and it shows that you've thought about it a bit.
Eric Lippert goes with the explicit form, and I'm starting to lean that way too.
See http://csharpindepth.com/viewnote.aspx?noteid=54 for a little bit more on this.
It's for you (and future maintainers), not the compiler.
Explicitness. I never use the default and always explicitly add the modifier.
This could be because of my Java background where the default was 'package' (roughly equivalent to 'internal' in C#) and so the difference always bothered me. I found explicitness to be preferable.
I also use ReSharper now which defaults to being explicit, so it only confirms and reinforces my bias :)
The private modifier explains intent.
A private member variable is not intended for direct manipulation outside the class. get/set accessors may or may not be created for the variable.
A private method is not intended for use outside the class. This may be for internal functionality only. Or you could make a default constructor private to prevent the construction of the class without passing in values.
The private modifier (and others like it) can be a useful way of writing self documenting code.
As pointed out by Jon Skeet in his book C# In Depth, there is one place in C# where the private keyword is required to achieve an effect.
If my memory serves correctly, the private keyword is the only way to create a privately scoped property getter or setter, when its opposite has greater than private accessibility. Example:
public bool CanAccessTheMissileCodes
{
get { return canAccessTheMissileCodes; }
private set { canAccessTheMissileCodes = value; }
}
The private keyword is required to achieve this, because an additional property accessability modifier can only narrow the scope, not widen it. (Otherwise, one might have been able to create a private (by default) property and then add a public modifier.)
Private is only the default for methods on a type, but the private modifier is used elsewhere.
From C# Language Specification 3.0 (msdn) Section 3.5.1
Depending on the context in which a member declaration takes place, only certain types of declared accessibility are permitted. Furthermore, when a member declaration does not include any access modifiers, the context in which the declaration takes place determines the default declared accessibility.
Namespaces implicitly have public declared accessibility. No access modifiers are allowed on namespace declarations.
Types declared in compilation units or namespaces can have public or internal declared accessibility and default
to internal declared accessibility.
Class members can have any of the five kinds of declared accessibility and default to private declared accessibility. (Note that a type declared as a member of a class can have any of the five kinds of declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.)
Struct members can have public, internal, or private declared accessibility and default to private declared accessibility because structs are implicitly sealed. Struct members introduced in a struct (that is, not inherited by that struct) cannot have protected or protected internal declared accessibility. (Note that a type declared as a member of a struct can have public, internal, or private declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.)
Interface members implicitly have public declared accessibility. No access modifiers are allowed on interface member declarations.
Enumeration members implicitly have public declared accessibility. No access modifiers are allowed on enumeration member declarations.
For completenes. And some people actually prefer to be explicit in their code about the access modifiers on their methods.
For symmetry and to conform with coding styles that like everything to be explicit (personally I like it ...)
Using private explicitly signals your intention and leaves clues for others who will support your code ;)
Some coding styles recommend that you put all the "public" items first, followed by the "private" items. Without a "private" keyword, you couldn't do it that way around.
Update: I didn't notice the "c#" tag on this so my answer applies more to C++ than to C#.
I usually leave private out but I find it useful for lining up code:
private int x;
public string y;
protected float z;
VS:
int x;
public string y;
protected float z;
As Robert Paulson said in his answer, the private modifier is not just used on members, but also on types. This becomes important because the default for types is internal which can leak unintentionally if you use the InternalsVisibleToAttribute.
Actually, if the class or struct is not declared with an access modifier it defaults to internal.
So if you want to make it private, use private.

Categories

Resources