Sara Molavi
Sara Molavi

Reputation: 49

printing list items c#

Hi I'm trying to show a list in output but I'm having problems with printing my list items. here is my code

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

I want the output to be like mahta sahabi 3244 niki fard 5411 . . . what should I do?

Upvotes: 0

Views: 109

Answers (1)

FakeCaleb
FakeCaleb

Reputation: 993

Just reference the student with its properties

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

Upvotes: 4

Related Questions