John Tan
John Tan

Reputation: 1385

non-static classes in static classes

What is the rationale that non-static inner classes can be declared within a static class?

public static class A
{
    public class B
    {
        public B() { }
    }
}

As compared to the fact that non-static members and functions cannot be declared within a static class.

Upvotes: 4

Views: 79

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156978

What about multiple instances of B? In that way, non-static classes nested in static classes are no different to regular non-static classes.

public static class A
{
    public class B
    {
        public B() { }
    }

    private static B b1 = new B();
    private static B b2 = new B();
}

See for a real-life example the static System.Linq.Enumerable class, which contains some specific implementations as nested non-static classes.

Upvotes: 3

Related Questions