Reputation: 5905
I tried to make a class as private and got this Error "Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal"
I got its meaning but I want to ask why this is not allowed? Are all access modifires not applicable on Class? Why I can't make a class private, protected or protected internal?
Upvotes: 63
Views: 37862
Reputation: 18141
As Abatonime said, you can only use public
or internal
in the Namespace level.
private
, protected
, or protected internal
can only be used in the Class level.
This works
namespace X
{
class A
{
// class code here
private class B // this class is an inner class
{
// class code here
}
}
}
This won't
namespace X
{
class A
{
// class code here
}
private class B // this is a class inside a namespace
{
// class code here
}
}
Upvotes: 5
Reputation: 583
In real world we are focus on visible object Once object is visible then we talk about scope of the object
example in real world
If you walking on street, you see houses in a colony colony has houses. If colony is protected no one can't able to see houses It is consider that no colony no houses is present
In Programming
If we make class as private/ protected at top-level no one known about it
is it present in assembly ?
please correct me, if i am out of the scope
Upvotes: 0
Reputation: 11
I had this same problem because I was creating a custom DLL and only wanted certain classes to be visible to an application using the DLL. So I just remove the modifier completely for classes I wanted to be private (within specific namespaces). The classes remained accessible to other classes within the same namespace in the DLL but did not show up in Intellisense in the calling application. No need for nested classes. The only explanation I can think of is the error message says cannot "explicitly" declare private...it doesn't say anything about implicitly.
namespace SmartCardAuthentication
{
class SmartCardIdentity : IIdentity
{
private string _firstName;
private string _lastName;
private string _middleInitial;
....
}
}
In example code above, class "SmartCardIdentity" is available to other class within same namespace, but not available to calling application when this class is rolled into a DLL. I have not tested it anyother way (i.e. visibility from a class in a different namespace within the DLL.).
Upvotes: 1
Reputation: 1
Only Public and Internal are applicable when defining class. If no access modifier is defined before the class default is internal.
refer to MSDN - [https://msdn.microsoft.com/en-us/library/8fd16xs0(v=vs.90).aspx]
Upvotes: 0
Reputation: 11
The default accessibility of top-level types is internal
.
The default accessibility of class and struct members is private
.
The only possible accessibility of interface and enum members is public
.
So a class is by default private
, and if you want to access that, you have to put public
before that.
Upvotes: 0
Reputation: 1898
Because it doesn't make sense. There's no way you can access protected or private classes defined at namespace level, only as nested classes.
Upvotes: 3
Reputation: 2185
Only nested classes could be declared as private. Not nested classes can be only public or internal (implicit without modifiers)
Upvotes: 2
Reputation: 6086
Because private means that the member is only visible in the containing class. Since a top-level class has no class containing it it cannot be private (or protected). (Internal or public are valid modifiers though).
What would you want private to mean on a top-level class?
Of course all modifiers apply to nested classes, i.e. a class defined within another class.
Upvotes: 80