打印列表项c#

时间:2016-10-19 11:01:02

标签: c# list printing

您好我正在尝试在输出中显示列表但我在打印列表项时遇到问题。这是我的代码

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 3244 尼基法德5​​411 。 。 。 我该怎么办?

1 个答案:

答案 0 :(得分:4)

只需引用学生的属性

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