如何在不打印列表位置的情况下打印记录列表的内容? [英] How do I print the contents of a list of records without it printing the position of the list?

查看:94
本文介绍了如何在不打印列表位置的情况下打印记录列表的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在Visual Studio 2017中将记录列表的内容打印到控制台中,但我无法打印列表的内容,只是列表在我的代码中的位置(据我所知) 。



以下是代码:



I have been trying to print the contents of a list of records into the console in Visual Studio 2017, but i can't get it to print the list's contents, just the list's position in my code (as i understand it).

Here is the code:

using System;
using System.Collections.Generic;



namespace list_of_records
{
    class Program
    {
        static void Main(string[] args)
        {
            List<StudentData> students = new List<StudentData>();

            StudentData student = new StudentData();
            
            for (int i = 0; i <= 1; i++ )
            {
                Console.WriteLine("first name: ");
                student.firstName = Console.ReadLine();
                Console.WriteLine("last name: ");
                student.lastName = Console.ReadLine();
                Console.WriteLine("age: ");
                student.age = Convert.ToInt32(Console.ReadLine());


                students.Add(student);
            }


            //students.ForEach(Console.WriteLine);
            /*
            foreach (var st in students)
            {
                Console.WriteLine(st);
            }
            */
            for (int i = 0; i < students.Count; i++)
            {
                Console.WriteLine(students[i]);
            }

        }



        public class StudentData
        {
            public string firstName;
            public string lastName;
            public int age;
        }

    }
}





打印:



it is printing:

list_of_records.Program+StudentData
list_of_records.Program+StudentData





这是什么我理解它在我的项目中的位置。



为什么会这样做?如何让它进行我的出价?



谢谢。



我的尝试:



你可以看到我在代码中尝试了什么,以及在互联网上找到的其他一些打印方法,但无济于事。



Which is as i understand it the position of the list in my project.

why does it do this and how can i make it do my bidding?

Thanks.

What I have tried:

you can see what i have tried in the code, and some other methods of printing that found on the internet, but to no avail.

推荐答案

那是因为你的 StudentData 类没有实现 ToString - 所以<的默认版本为<改为使用code> object ,并打印该类的全名(因为它不知道您添加的内容和要打印的内容)。

试试这个:

That's because your StudentData class doesn't implement ToString - so the default version for object is used instead, and that prints the full name of the class (because it doesn't "know" about the content you added and what you want printed).
Try this:
public class StudentData
    {
    public string firstName;
    public string lastName;
    public int age;
    public override string ToString()
        {
        return


{firstName} {lastName}是{age}岁;
}
}
"{firstName} {lastName} is {age} years old"; } }

或者更好,这个:

Or better, this:

public class StudentData
    {
    public string firstName { get; set; }
    public string lastName { get; set; }
    public int age { get; set; }
    public override string ToString()
        {
        return


{firstName} {lastName}是{age}岁;
}
}
"{firstName} {lastName} is {age} years old"; } }

因为您不应直接公开字段 - 请改用属性。

Because you shouldn't expose fields directly - use properties instead.


这篇关于如何在不打印列表位置的情况下打印记录列表的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆