如何插入列表<>;转换为构造函数

本文关键字:gt lt 转换 构造函数 列表 何插入 插入 | 更新日期: 2023-09-27 18:30:05

我想创建一个类构造函数,Telefono将其作为参数一个字符串插入到列表中

编译器I报告错误:"无法隐式转换为无效字符串"

我该如何修复?

 public static class Telefono
    {
        public double LevelBattery { get; set; }
        public List<String> NomeTelefono { get; set; }
        public bool TelefonoON { get; set; }
        public Telefono(string telefono)
        {
            telefono = NomeTelefono.Add(telefono); //ERROR!!!!
        }
        public void ON()
        {
            Random x = new Random();
            int Batt = x.Next(100);
            LevelBatty = Batt;
            if (Levelbattery > 10)
                TelefonoON = true;
            else
            {
                Console.WriteLine("Ricaricare subito il telefono {0}. Batteria inferiore al 10%", NomeTelefono);
                Console.ReadLine();
                TelefonoON = false;
            }
        }

如何插入列表<>;转换为构造函数

List<T>.Add不返回任何内容。我想你只想初始化你的列表并添加项目:

public Telefono(string telefono)
{
    NoneTelefono = new List<string>();
    NomeTelefono.Add(telefono);
}

您需要添加

NomeTelefono = new List<string>();

在构造函数中