打印列表项c#

本文关键字:列表 打印 | 更新日期: 2023-09-27 18:13:39

你好,我试图在输出中显示一个列表,但我有打印我的列表项目的问题。这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace educationsystem
{
    public class student 
    {
        public string name { get;set;}
        public string lastname {get;set;}
        public long phone {get;set;}
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<student> Students = new List<student>();
            Students.Add(new student {
                name = "mahta",
                lastname = "sahabi",
                phone = 3244
            });
            Students.Add(new student { 
                name = "niki", 
                lastname = "fard", 
                phone = 5411 
            });
            Students.Add(new student { 
                name = "hana", 
                lastname = "alipoor", 
                phone = 6121 
            });
            foreach(student students in students)
                Console.WriteLine(name+" "+lastname+" "+phone);
            Console.ReadKey();
        }
    }
}

我希望输出是这样的Mahta sahabi 3244Niki fard 5411...我该怎么办?

打印列表项c#

用它的属性引用student

foreach (var student in students)
{
    Console.Write(student.name + " " + student.lastname + " " + student.phone); 
}