Используя структуру создать списки студентов - C#
Формулировка задачи:
Используя структуру создать списки студентов группы и вывести отдельно список юношей и список девушек.
Решение задачи: «Используя структуру создать списки студентов»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public enum Sex { Male, Female }
struct Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Sex Sex { get; set; }
public Student(string firstName, string lastName, Sex sex):this()
{
FirstName = firstName;
LastName = lastName;
Sex = sex;
}
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student("Иванов","Иван",Sex.Male),
new Student("Кузнецова","Мария",Sex.Female),
new Student("Петрова","Анастасия",Sex.Female),
new Student("Сидоров","Никита",Sex.Male),
new Student("Козлов","Антон",Sex.Male),
new Student("Павлова","Ольга",Sex.Female),
};
foreach (var group in students.GroupBy(s => s.Sex))
{
foreach (var stud in group)
{
Console.WriteLine(stud);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}