Tuesday, May 10, 2011

Access specifiers in .NET


http://stackoverflow.com/questions/2521459/what-are-the-default-access-modifiers-in-c
http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx
http://msdn.microsoft.com/en-us/library/ms173121.aspx
==================================================================



http://stackoverflow.com/questions/2521459/what-are-the-default-access-modifiers-in-c


top level class: internal
method
: private
members
(unless an interface or enum): private (including nested classes)
members
(of interface or enum): public
constructor
: protected delegate: internal interface: internal
explicitly implemented
interface member: public! 



Class and Struct Accessibility
Internal is the default if no access modifier is specified.
Class and Struct Member Accessibility
Class members (including nested classes and structs) can be declared with any of the five types of access. Struct members cannot be declared as protected because structs do not support inheritance.
The accessibility of a member can never be greater than the accessibility of its containing type
User-defined operators must always be declared as public. For more information, see operator (C# Reference).
Destructors  and namespaces cannot have accessibility modifiers.
Other Types
Interfaces declared directly with a namespace can be declared as public or internal and like classes and structs, interfaces default to internal access.
Enumeration members are always public, and no access modifiers can be applied.
By default, delegates have internal access.



Non-nested (may only have internal or public accessibility) types, enumeration and delegate accessibilities
                     | Default   | Permitted declared accessibilities ------------------------------------------------------------------ 
namespace            | public    | none (always implicitly public) 
 enum                 | public    | none (always implicitly public) 
 interface            | internal  | public, internal 
 class                | internal  | public, internal 
 struct               | internal  | public, internal 
 delegate             | internal  | public, internal 
Nested type and member accessiblities
                     | Default   | Permitted declared accessibilities ------------------------------------------------------------------ 
namespace            | public    | none (always implicitly public) 
 enum                 | public    | none (always implicitly public) 
 interface            | public    | none 
 class                | private   | All¹ 
 struct               | private   | public, internal, private² 
 delegate             | private   | All¹ 
 
constructor          | protected | All¹ 
 interface member     | public    | none (always implicitly public) 
 
method               | private   | All¹ 
 
field                | private   | All¹ 
 
user-defined operator| none      | public (must be declared public) 
¹ All === public, protected, internal, private, protected internal
² structs cannot inherit from structs or classes (although they can, interfaces), hence protected is not a valid modifier

http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

Only one access modifier is allowed for a member or type, except when you use the protected internal combination.
Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.



http://msdn.microsoft.com/en-us/library/ms173121.aspx

Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.
Enumeration members are always public, and no access modifiers can be applied.
Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.


The protected internal accessibility level means protected OR internal, not protected AND internal. In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.

No comments:

Post a Comment