3

I was repeating knowledge about nested classes and I saw this:

In C#, the user is allowed to inherit a nested class from the outer class

I wonder did any one ever used it? what could be valid way of using class that inherit like that?

class A
{
    class B:A
    {

    }
}
  • https://stackoverflow.com/questions/7544882/inheritance-nestedclasses-in-c-sharp – Renat Jul 16 '19 at 13:38
  • 4
    Possible duplicate of [Inheritance + NestedClasses in C#](https://stackoverflow.com/questions/7544882/inheritance-nestedclasses-in-c-sharp) – Kristian Barrett Jul 16 '19 at 13:39

2 Answers2

2

Nested classes can access all members of their enclosing type, including the constructors. You can use a private constructor to create a class with a fixed number of subclasses.

    public abstract class ResponseCode
    {
        public abstract int NumericCode { get; }

        private ResponseCode() { }

        public sealed class Success : ResponseCode
        {
            public override int NumericCode => 200;
        }

        public sealed class Error : ResponseCode
        {
            public override int NumericCode => 500;
        }
    }
Joe Sewell
  • 6,067
  • 1
  • 21
  • 34
0
  • The scope of a nested class is bounded by the scope of its enclosing class.
  • By default, the nested class is private.
  • In C#, a user is allowed to inherit a class (including nested class) into another class.

https://www.geeksforgeeks.org/nested-classes-in-c-sharp/

Thameem
  • 700
  • 1
  • 13
  • 38