C#如何将Console.ReadLine数据存入List 解决Client类无Add方法报错
C#读取Console.ReadLine内容存入自定义List报错问题
我是编程新手,想请教一个问题:我目前无法将Console.ReadLine读取到的数据存入自定义List中,我正在尝试提升编程能力,但总容易把问题复杂化,不知道当前写法是否存在明显错误?
原有代码
using System; using System.Collections.Generic; namespace BB1 { public class Client { public string Telefon { get; set; } public string Navn { get; set; } public string Addresse { get; set; } public int Postnummer { get; set; } public string By { get; set; } public string Email { get; set; } List<string> client = new List<string>(); public void AddToList() { client.Add("Navn" + Navn + "Telefon" + Telefon); } public static void Main(string[] args) { var client = new Client(); int tl; Console.WriteLine("Indtast dit navn her"); client.Navn = Console.ReadLine(); Console.Write("Indtast dit TLF nummer: "); client.Telefon = Console.ReadLine(); bool success = int.TryParse(client.Telefon, out tl); while (!success) { Console.WriteLine("Du har indtastet et ugyldigt nummer"); client.Telefon = Console.ReadLine(); success = int.TryParse(client.Telefon, out tl); } Console.WriteLine("TEST to see if client list is updated"); Console.ReadKey(); Console.WriteLine(client); Console.ReadKey(); } } }
尝试修改后的代码
List<Client> client = new List<Client>(); public override string ToString() { return "Client: " + Navn + " " + Telefon; } public static void Main(string[] args) { var client = new Client(); int tl; Console.WriteLine("Indtast dit navn her"); client.Navn = Console.ReadLine(); Console.Write("Indtast dit TLF nummer: "); client.Telefon = client.Add(Console.ReadLine());
运行后报如下错误:
'Client' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'Client' could be found (are you missing a using directive or an assembly reference)
我已经调试这个问题一整天十分疲惫,请问该如何解决?
问题原因
- 命名冲突:你在Client类里定义了一个
List<Client>类型的成员变量叫client,同时在Main方法里又实例化了一个Client类型的变量也叫client,你调用client.Add的时候,实际访问的是Client类的实例,不是List实例,自然没有Add方法。 - 职责混淆:
List<Client>是用来存储多个Client对象的容器,不应该作为Client类的成员变量,应该放在Main方法里作为独立的全局容器使用。 - 逻辑错误:
client.Telefon = client.Add(Console.ReadLine());的写法完全不符合语法,List的Add方法返回值为void,不能赋值给string类型的Telefon属性。
修正后的完整代码
using System; using System.Collections.Generic; namespace BB1 { public class Client { public string Telefon { get; set; } public string Navn { get; set; } public string Addresse { get; set; } public int Postnummer { get; set; } public string By { get; set; } public string Email { get; set; } // 重写ToString方法输出客户端信息 public override string ToString() { return $"Client: {Navn} {Telefon}"; } public static void Main(string[] args) { // 把List<Client>移到此处作为存储所有客户的独立容器 List<Client> clientList = new List<Client>(); var client = new Client(); int tl; Console.WriteLine("Indtast dit navn her"); client.Navn = Console.ReadLine(); Console.Write("Indtast dit TLF nummer: "); client.Telefon = Console.ReadLine(); bool success = int.TryParse(client.Telefon, out tl); while (!success) { Console.WriteLine("Du har indtastet et ugyldigt nummer"); client.Telefon = Console.ReadLine(); success = int.TryParse(client.Telefon, out tl); } // 将填好信息的client对象加入到列表中 clientList.Add(client); Console.WriteLine("TEST to see if client list is updated"); Console.ReadKey(); // 输出列表中的第一个元素信息 Console.WriteLine(clientList[0]); Console.ReadKey(); } } }
修改后你之前输出client对象看不到详细信息的问题也同步解决,现在可以正常输出存入的姓名和电话信息。如果需要存储多个客户,只需要循环执行录入逻辑,每次new一个新的Client对象填好信息后加入List即可。
内容的提问来源于stack exchange,提问作者Daniel Rønde
相关产品推荐
相关产品推荐

