“Public” nested classes or not

后端 未结 6 1762
离开以前
离开以前 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:23

    Another practical example that I have for a valid use of public nested classes is in MVC pattern when I use a viewmodel with an IEnumerable property. for example:

    public class OrderViewModel
    {
    public int OrderId{ get; set; }
    public IEnumerable Products{ get; set; }
    
    public class Product {
    public string ProductName{ get; set; }
    public decimal ProductPrice{ get; set; }
    }
    
    }
    

    I use it because I don't want Product class to be re-used outside because it is customized only for that specific viewmodel which contains it. But I can't make it private because the Products property is public.

提交回复
热议问题