简化或减少代码大小

时间:2016-09-21 09:57:56

标签: c# get set boolean

我在缩短应用程序中的代码时遇到问题。我有这个示例代码,我想让它变小。该列表包含其他内容,如模型,连续出版物等。我只需要更清洁/可读。 Ofc,代码比这要大得多:

public class SmartPhones
{
    public PhoneSettings()
    {
        Sony = true;
        Htc = true;
        Nokia = true;
        Iphone = true;

        SonyColor = Color Blue;
        HtcColor = Color Black;
        NokiaColor = Color Red;
        IphoneColor = Color White;
    }
    public ToggleButton Sony {get; set;}
    public ToggleButton Htc {get; set;}
    public ToggleButton Nokia {get; set;}
    public ToggleButton Iphone {get; set;}

    public ColorButton SonyColor {get; set;}
    public ColorButton HtcColor {get; set;}
    public ColorButton NokiaColor {get; set;}
    public ColorButton IphoneColor {get; set;}
}

谢谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

我不太明白你想要实现什么,但对我来说,创建具有所有属性的Phone类似乎更合乎逻辑,然后创建包含具有适当属性的所有Phone实例的SmartPhones类。 像这样的东西

    public class Phone
    {
        public string Type { get; set; }
        public string ToggleButton { get; set; }
        public string ColorButton { get; set; }
    }

    public class SmartPhones
    {
        private List<Phone> _list = new List<Phone>();

        public SmartPhones()
        {
            _list.Add(new Phone { Type = "Htc", ToggleButton = "aa", ColorButton = "bb"});
            _list.Add(new Phone { Type = "Nokia", ToggleButton = "aa", ColorButton = "bb" });
            _list.Add(new Phone { Type = "Sony", ToggleButton = "aa", ColorButton = "bb" });
        }
    }