Как пройти массив структур в цикле? - C#
Формулировка задачи:
Создать массив, в котором записать информацию об учениках класса: фамилию, имя, дату рождения (в виде структуры с полями день, месяц, год), список увлечений(строка). Считать с клавиатуры год и вывести перечень фамилий и имен учеников, родившихся в этот год и их увлечения.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace x
{
struct Student
{
string name,lastName;
string interest;
Date date;
public Student(string name,string lastName,string interest, int year,int month,int day)
{
this.name = name;
this.lastName = lastName;
this.interest = interest;
date = new Date(year, month, day);
}
public void GetInfo(Student student)
{
for(int i=0;i<=)
Console.WriteLine(student.name + " "+student.lastName+" "+student.interest);
}
}
struct Date
{
int year;
int month;
int day;
public Date(int year, int month, int day)
{
this.year = year; this.month = month; this.day = day;
}
}
class Program
{
static void Main(string[] args)
{
}
}
}Решение задачи: «Как пройти массив структур в цикле?»
textual
Листинг программы
using System;
using System.Linq;
namespace ConsoleApplication96
{
class Program
{
struct Student
{
public string Name { get; private set; }
public string LastName { get; private set; }
public string Interest { get; private set; }
public DateTime Date { get; private set; }
public Student(string name, string lastName, string interest, int year, int month, int day)
{
this.Name = name;
this.LastName = lastName;
this.Interest = interest;
Date = new DateTime(year, month, day);
}
public override string ToString()
{
return $"{Name} {LastName} {Interest}";
}
}
static void Main(string[] args)
{
Student[] st = new Student[]
{
new Student("Вася","Пупкин","бухать",1990,3,2),
new Student("Ваня","Петров","курить",1991,3,2),
new Student("Женя","Иванов","нюхать",1991,3,2),
new Student("Дима","Сидоров","колоться",1993,3,2)
};
int year = Int32.Parse(Console.ReadLine());
foreach(var student in st.Where(x => x.Date.Year == year))
{
Console.WriteLine(student);
}
Console.Read();
}
}
}