Прокомментировать и разобрать код - C#
Формулировка задачи:
Здравствуйте. Объясните, пожалуйста, как работает этот код?
Если можно, прокомментируйте, пожалуйста, каждую функцию. Заранее, спасибо огромное.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>()
{
new Student(){FirstName="Alex",SecondName="Simpson",Group=10},
new Student(){FirstName="Will",SecondName="Smith",Group=25}
};
//Сериализуем
SerializeList(list);
Console.WriteLine("Operation completed!");
Console.WriteLine("Deserialized from file: ");
//Десериализуем
List<Student> ListFromFile = GetStudents();
//Выводим на консоль
foreach (Student s in ListFromFile)
{
Console.WriteLine("First name: {0}, Second name: {1}, Grop: {2}",
s.FirstName, s.SecondName, s.Group);
}
Console.ReadLine();
}
static void SerializeList(List<Student> list)
{
XmlSerializer xmlSerial = new XmlSerializer(typeof(List<Student>));
using (Stream fs = new FileStream(@"C:\MyStudents.xml", FileMode.Create,
FileAccess.Write, FileShare.None))
{
xmlSerial.Serialize(fs, list);
}
}
static List<Student> GetStudents()
{
List<Student> tempList;
XmlSerializer xmlSerial = new XmlSerializer(typeof(List<Student>));
using (Stream fs = new FileStream(@"C:\MyStudents.xml", FileMode.Open,
FileAccess.Read, FileShare.None))
{
tempList = (List<Student>)xmlSerial.Deserialize(fs);
}
return tempList;
}
}
[Serializable]
public class Student
{
public string FirstName { get; set; }
public string SecondName { get; set; }
public int Group { get; set; }
}
}Решение задачи: «Прокомментировать и разобрать код»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
//обьявление и инициализация обьекта класса List, который может содержать список обьектов Student, а так же добавление в него двух Student
List<Student> list = new List<Student>()
{
new Student(){FirstName="Alex",SecondName="Simpson",Group=10},
new Student(){FirstName="Will",SecondName="Smith",Group=25}
};
//Сериализуем
SerializeList(list); //Вызов функции SerializeList и передача ей аргументом ранее созданый список
Console.WriteLine("Operation completed!");
Console.WriteLine("Deserialized from file: ");
//Десериализуем
List<Student> ListFromFile = GetStudents(); //Вызов функции которая должна возвращать список студентов
//Выводим на консоль
foreach (Student s in ListFromFile)
{
Console.WriteLine("First name: {0}, Second name: {1}, Grop: {2}",
s.FirstName, s.SecondName, s.Group);
}
Console.ReadLine();
}
//Функция сериализации (сохранения списка студентов в xml файл)
static void SerializeList(List<Student> list) // обьявление функции SerializeList, которая принимает аргументом список обьектов класса Student
{
XmlSerializer xmlSerial = new XmlSerializer(typeof(List<Student>)); // Обьвявляем xml сериализатор
using (Stream fs = new FileStream(@"C:\MyStudents.xml", FileMode.Create,
FileAccess.Write, FileShare.None))
{
xmlSerial.Serialize(fs, list); // сериализуем список в xml (сохранение списка в xml файл)
}
}
//Функция десериализации (чтение списка студентов и xml файла)
static List<Student> GetStudents() // обьявление функции GetStudents которая возвращает список Student
{
List<Student> tempList;
XmlSerializer xmlSerial = new XmlSerializer(typeof(List<Student>)); //создаем сериализатор
using (Stream fs = new FileStream(@"C:\MyStudents.xml", FileMode.Open,
FileAccess.Read, FileShare.None))
{
tempList = (List<Student>)xmlSerial.Deserialize(fs); // десериализуем xml в список обьектов Student (читаем список студентов из файла)
}
return tempList;
}
}
[Serializable]
// Обьявление класса Student
public class Student
{
public string FirstName { get; set; }
public string SecondName { get; set; }
public int Group { get; set; }
}
}