【问题标题】:printing list items c#打印列表项 c#
【发布时间】:2016-10-19 11:01:01
【问题描述】:

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

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();
        }
    }
}

我希望输出像 第3244章 尼基法德 5411 . . . 我该怎么办?

【问题讨论】:

  • 您在打印输出时没有引用“学生”,只是引用它的属性。此外,您还写了“foreach(学生中的学生)”。这是一个错字吗?实际上应该是“学生中的学生”?

标签: c# list printing


【解决方案1】:

只需引用学生及其属性

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多