C# 如何将列表插入构造函数中?
我想创建一个类构造函数,将Telefono作为一个参数插入到列表中 编译器I报告错误:无法隐式转换为无效字符串 我该怎么修理C# 如何将列表插入构造函数中?,c#,C#,我想创建一个类构造函数,将Telefono作为一个参数插入到列表中 编译器I报告错误:无法隐式转换为无效字符串 我该怎么修理 public static class Telefono { public double LevelBattery { get; set; } public List<String> NomeTelefono { get; set; } public bool TelefonoON { get; set
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;
}
}
添加不会返回任何内容。我想您只需要初始化列表并添加以下项目:
public Telefono(string telefono)
{
NoneTelefono = new List<string>();
NomeTelefono.Add(telefono);
}
您需要添加
NomeTelefono = new List<string>();
在构造函数中NomeTelefono为空。在使用之前初始化它…该错误似乎与字符串插入无关。你确定是他在抱怨那句台词吗?虽然您需要实例化列表,但您将得到一个NRE运行时错误。很好!我错过了add中的作业。