C#实例化类对象&;添加到列表中

C#实例化类对象&;添加到列表中,c#,C#,如何创建Country的新实例,该实例使用3个变量firstParam、secondParam、thirdParam作为其构造函数并添加到列表countryList 从.txt读取这3个变量,并在, 阿富汗,喀布尔,30419928 阿尔巴尼亚,地拉那,1977年 我的3个拆分变量firstParam、secondParam、thirdParam包含正确的信息。当我添加到countryList时,我认为我犯了一个语法错误,因为调试模式通过变量加载正确的信息,但是当我在countryList上尝试

如何创建Country的新实例,该实例使用3个变量firstParam、secondParam、thirdParam作为其构造函数并添加到列表countryList

从.txt读取这3个变量,并在, 阿富汗,喀布尔,30419928 阿尔巴尼亚,地拉那,1977年

我的3个拆分变量firstParam、secondParam、thirdParam包含正确的信息。当我添加到countryList时,我认为我犯了一个语法错误,因为调试模式通过变量加载正确的信息,但是当我在countryList上尝试foreach时,我得到了输出“countries.country”

List countryList=新列表();
foreach(行中的字符串行)
{
string[]条件=line.Split(',');
字符串firstParam=“”;
字符串secondParam=“”;
int thirdParam=1;
firstParam=标准[0];
secondParam=标准[1];
thirdParam=Convert.ToInt32(标准[2]);
countryList.Add(新国家/地区(第一参数、第二参数、第三参数));
}
我没有主意了,不知道还有什么可以尝试。我正在努力学习更多关于作文的知识,但希望有人能推动我前进。
谢谢。

尝试以下方法以验证您的代码是否按您希望的方式工作:

List<Country> countryList = new List<Country>();

foreach (string line in lines)
{
    string[] criteria = line.Split(',');

    countryList.Add(new Country(criteria[0],
                                criteria[1],
                                Convert.ToInt32(criteria[2]))
}

Console.WriteLine($"Countries in countryList: {countryList.Count}.");

foreach ( Country country in countryList)
{
    Console.WriteLine($"Name: {country.Name}, Capital: {country.Capital}, Population: {country.Population}.");
}
List countryList=新列表();
foreach(行中的字符串行)
{
string[]条件=line.Split(',');
countryList.Add(新国家/地区)(标准[0],
标准[1],
转换为32(标准[2]))
}
Console.WriteLine($“countryList中的国家:{countryList.Count}”);
foreach(国家列表中的国家)
{
Console.WriteLine($“Name:{country.Name},Capital:{country.Capital},Population:{country.Population}”);
}

您可能需要为Country类的字段插入自己的名称,而不是名称、大写字母和人口。

如果要选择用于调试的值,应使用这种方式“Override ToString()”或“DebuggerDisplayAttribute”

 //DebuggerDisplayAttribute
[DebuggerDisplay("Value = {Display}")]
public class Country
{
    public string Name { get; set; }
    public string City { get; set; }    

    public string Display
    {
        get => $"{Name}, {City}";
    }

    //Override ToString()
    public override string ToString()
    {
        return $"{Name}, {City}";
    }
}

请向我们展示您用于获取输出的代码(countryList上的foreach)。显示打印输出的代码。除了没有对输入进行任何验证外,该代码看起来还可以,只是您可以组合三个参数变量的声明和实例化。在接下来的几行中重新分配时,无需将它们分配给默认值。要打印countryList,我使用了
codeach(countryList中的Country singleCountry)
{
Console.WriteLine(singleCountry.Name);
//如果使用Console.WriteLine(firstParam),我尝试了不使用.Name的方法;我可以正确地看到变量的个数,因此问题是如何添加到列表中或如何打印列表。我将尝试建议的重载方法,然后重新阅读所有注释:d当我对每个变量进行排序以查看我的国家列表中的内容时,输出仅显示“countries.country”对于每个条目。如果我使用foreach并使用.Name,我会得到一个空白结果。没有错误,只有空白控制台。
code foreach(countryList中的国家项目){console.WriteLine(item.Name);console.WriteLine(item);}
为什么换行不起作用我用两个空格结束这行,我试过了
我想我有一个更好的问题。有人能告诉我如何将一个类的新实例添加到列表中吗?我知道firstParam、secondParam和thirdParam三个变量都包含正确的信息。
所以问题是我将实例化对象添加到列表的方式。不应该只返回
Display
 //DebuggerDisplayAttribute
[DebuggerDisplay("Value = {Display}")]
public class Country
{
    public string Name { get; set; }
    public string City { get; set; }    

    public string Display
    {
        get => $"{Name}, {City}";
    }

    //Override ToString()
    public override string ToString()
    {
        return $"{Name}, {City}";
    }
}