Что делает этот код? - C# (191372)
Формулировка задачи:
Здравствуйте. Объясните, пожалуйста, как работает этот код?
Если можно, прокомментируйте, пожалуйста, каждую функцию. Заранее, спасибо огромное.
Листинг программы
- 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> list = new List<Student>() // список из экземпляров класса Студент
- {
- new Student(){FirstName="Alex",SecondName="Simpson",Group=10}, //добавляем 2 экземпляра в список
- 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>)); // создаем ХМЛ сериалайзер, который в своем конструкторе принимает экземпляр класса Type, который мы получаем используя оператор typeof
- 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; }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д