1
  1. If we do not specify Public/Private/Protected, what will it be?
  2. Is there something known as a private class?
DIF
  • 2,470
  • 6
  • 35
  • 49
user3398315
  • 331
  • 6
  • 17

4 Answers4

3

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 {

    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

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
      {
      }
}
Bharadwaj
  • 2,535
  • 1
  • 22
  • 35
1

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
        }
    }
}
Community
  • 1
  • 1
Codor
  • 17,447
  • 9
  • 29
  • 56
0

What are the Default Access Modifiers in C#?

and there is a private class but in my opinion its pointless

Community
  • 1
  • 1