简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM