“Public” nested classes or not

后端 未结 6 1777
离开以前
离开以前 2020-12-24 16:03

Suppose I have a class \'Application\'. In order to be initialised it takes certain settings in the constructor. Let\'s also assume that the number of settings is so many th

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 16:06

    You can use namespaces to relate things that are... related.

    For example:

    namespace Diner
    {
        public class Sandwich
        {
            public Sandwich(Filling filling) { }
        }
    
        public class Filling { }
    }
    

    The advantage of this over using classes as if they were namespaces is that you can optionally use using on the calling side to abbreviate things:

    using Diner;
    
    ...
    
    var sandwich = new Sandwich(new Filling());
    

    If you use the Sandwich class as if it were a namespace for Filling, you have to use the full name Sandwich.Filling to refer to Filling.

    And how are you going to sleep at night knowing that?

提交回复
热议问题