Why elements defined in a namespace cannot be explicitly declared?
Solution 1:
Elements defined in a namespace may be explicitly declared public or internal.
They may not be explicitly declared private or protected (or protected internal) because these modifiers only make sense for members of a class.
Your protected class GetDataBL
, for example, makes no sense, because "protected" means "accessible to classes that inherit from the containing class" -- but there is no containing class for GetDataBL.
Solution 2:
private
protected
means they will be accessible to this class or to the derived classes.
In the Namespace level there is no class to derived from so it useless.
You can use only public
or internal
in the Namespace
level
MSDN docs
Solution 3:
(I believe you'll actually get a compile-time error; if you're only seeing this at execution time, then chances are your code is being compiled at execution time too, e.g. as part of a web app. Logically it's a compile-time error, not an exception.)
The protected
access modifier (loosely) makes a member accessible to a derived containing type; but in the case of a namespace member there is no containing type.
Likewise a private member's accessibility domain is the program text of the containing type - and again, there is no containing type.
What are you actually trying to achieve by making GetDataBL
protected?
Solution 4:
It's the scoping of the elements that is causing the error, as explained by the error - and the C# specification (ECMA section 10.5.1):
- Types declared in compilation units or namespaces can have
public
orinternal
declared accessibility and default tointernal
declared accessibility.- Class members can have any of the five kinds of declared accessibility and default to
private
declared accessibility.- Struct members can have
public
,internal
, orprivate
declared accessibility and default toprivate
declared accessibility because structs are implicitly sealed.