- If we do not specify Public/Private/Protected, what will it be?
- Is there something known as a private class?

- 2,470
- 6
- 35
- 49

- 331
- 6
- 17
4 Answers
1: that depends on whether the class is nested or not. A top level class defaults to internal
. A nested class defaults to private
.
class TopLevelClass {
class PrivateClass {
}
}
2: yes, but only for nested classes:
class TopLevelClass {
private class ExplicitlyPrivateClass {
}
class ImplicitlyPrivateClass {
}
}

- 1,026,079
- 266
- 2,566
- 2,900
If you don't specify Public/Private/Protected
for a main class
it will be internal
, for a nested class the default access specifier will be private
.
private class
exists. You can access a private class
only if it is declared inside another class. Means it is private
to the parent class as
class example //which is default interal
{
private class ex
{
}
class ex1 //default private
{
}
}

- 2,535
- 1
- 22
- 35
-
2"If you don't specify Public/Private/Protected it will be internal." that too, depends on whether the type is nested – Marc Gravell Apr 22 '14 at 06:46
-
@MarcGravell I have updated :) – Bharadwaj Apr 22 '14 at 06:55
1) If no modifier is specified, the visibility will depend on the situation where it is omitted; the topic is discussed in this question.
2) In the following code, InnerClass
is private
in OuterClass
.
namespace ClassTest
{
class OuterClass
{
private class InnerClass
{
}
OuterClass()
{
InnerClass Test = new InnerClass();
}
}
class Program
{
static void Main(string[] args)
{
OuterClass TestOne = new OuterClass();
InnerClass TestTwo = new InnerClass(); // does not compile
}
}
}
-
1" If no modifier is specified, the visibility will be private. " - no, in virtually all cases, the visibility will be `internal`. – Marc Gravell Apr 22 '14 at 06:47
-
-
"1) If no modifier is specified, the visibility will be internal." - still wrong, for the reason that you explain in 2 – Marc Gravell Apr 22 '14 at 06:57
-
Indeed, thanks for the hint again; I updated the answer to refer to a related question. – Codor Apr 22 '14 at 07:12
What are the Default Access Modifiers in C#?
and there is a private class but in my opinion its pointless

- 1
- 1