简化或减少代码大小

本文关键字:代码 | 更新日期: 2023-09-27 18:07:58

我在应用程序中有一个缩短代码的问题。我有这个示例代码,我想让它更小。该列表包含其他内容,如模型,序列等。我只需要使更干净/可读。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;}
}

感谢所有的小帮助!

简化或减少代码大小

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

    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" });
        }
    }