打印列表< object>在控制台C#中 [英] print list<object> in console c#
本文介绍了打印列表< object>在控制台C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试在c#中打印对象列表时,它无法打印我想要的内容,让我演示一下.
when i try to print a list of objects in c# it does not print what i want, let me demonstrate.
示例:
public class Classname
{
public string hello;
public string world;
}
namespace inloggning
{
class Program
{
static void Main(string[] args)
{
List<object> listOfObjects = new List<object>();
Classname example = new Classname();
example.hello = "hello";
example.world = "world";
listOfObjects.Add(example);
Console.WriteLine(listOfObjects[0]);
}
}
}
当我运行此命令时,它会打印出"inloggning.Classname".有人可以帮助我了解为什么以及如何解决此问题,以便打印出"hello"字样.和世界".
when i run this it prints out "inloggning.Classname". Can someone help me understand why and how i can fix it so it prints "hello" and "world".
推荐答案
像这样向您的对象添加toString方法
Add a toString method to your object, like that
public string overide ToString()
{
return hello + " " + world;
}
这篇关于打印列表< object>在控制台C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文