从列表中打印出对象元素
Print out object elements from list
我试图在主范围内使用 foreach
循环打印列表中的对象元素,但唯一打印出来的是:
ConsoleApplication1.WorldMap
这里出了什么问题,我如何让它打印出实际元素?
using ConsoleApplication1;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class WorldMap
{
private string higher;
private string lower;
public worldMap()
{
}
public WorldMap(string higher, string lower)
{
this.higher = higher;
this.lower = lower;
}
public string Higher
{
get { return higher; }
set { higher = value; }
}
public string Lower
{
get { return Lower; }
set { Lower = value; }
}
}
class Program
{
static void Main(string[] args)
{
WorldMap map = new WorldMap();
List<WorldMap> mapList = new List<WorldMap>();
mapList.Add(new WorldMap(map.Lower, map.Higher));
Console.WriteLine("\tInsert highest point:");
map.Highest= Console.ReadLine();
Console.WriteLine("\tInsert lowest point");
map.Lowest= Console.ReadLine();
using (StreamWriter writer = new StreamWriter(@"C:\freq.txt", true))
{
writer.WriteLine(map.Lower + map.Higher);
}
foreach (object view in mapList)
{
Console.WriteLine(view);
}
}
}
}
视图是 WorldMap 类型,请改用它:
foreach (object view in mapList)
{
Console.WriteLine(view.Lower.ToString() + "," + view.Higher.ToString() );
}
因为您没有覆盖 WorldMap
的 ToString
方法,所以 Console.WriteLine(view)
returns class 的名称是 WorldMap
.您可以覆盖 class 中的 ToString
方法或调用 foreach
循环中的属性,但不要忘记您应该将 object
更改为 var
。像这样:
class WorldMap
{
....
....
public override string ToString()
{
return Lower + " " + Higher;
}
}
或(将object
更改为var
):
foreach (var view in mapList)
{
Console.WriteLine(view.Lower + " " + view.Higher);
}
我试图在主范围内使用 foreach
循环打印列表中的对象元素,但唯一打印出来的是:
ConsoleApplication1.WorldMap
这里出了什么问题,我如何让它打印出实际元素?
using ConsoleApplication1;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class WorldMap
{
private string higher;
private string lower;
public worldMap()
{
}
public WorldMap(string higher, string lower)
{
this.higher = higher;
this.lower = lower;
}
public string Higher
{
get { return higher; }
set { higher = value; }
}
public string Lower
{
get { return Lower; }
set { Lower = value; }
}
}
class Program
{
static void Main(string[] args)
{
WorldMap map = new WorldMap();
List<WorldMap> mapList = new List<WorldMap>();
mapList.Add(new WorldMap(map.Lower, map.Higher));
Console.WriteLine("\tInsert highest point:");
map.Highest= Console.ReadLine();
Console.WriteLine("\tInsert lowest point");
map.Lowest= Console.ReadLine();
using (StreamWriter writer = new StreamWriter(@"C:\freq.txt", true))
{
writer.WriteLine(map.Lower + map.Higher);
}
foreach (object view in mapList)
{
Console.WriteLine(view);
}
}
}
}
视图是 WorldMap 类型,请改用它:
foreach (object view in mapList)
{
Console.WriteLine(view.Lower.ToString() + "," + view.Higher.ToString() );
}
因为您没有覆盖 WorldMap
的 ToString
方法,所以 Console.WriteLine(view)
returns class 的名称是 WorldMap
.您可以覆盖 class 中的 ToString
方法或调用 foreach
循环中的属性,但不要忘记您应该将 object
更改为 var
。像这样:
class WorldMap
{
....
....
public override string ToString()
{
return Lower + " " + Higher;
}
}
或(将object
更改为var
):
foreach (var view in mapList)
{
Console.WriteLine(view.Lower + " " + view.Higher);
}