static void Main(string[] args) { XmlSerializer serializer = new XmlSerializer(typeof(Person)); string xml; using (StringWriter sw = new StringWriter()) { Person p = new Person() { FirstName = "John", LastName = "Doe", Age = 23, }; serializer.Serialize(sw, p); xml = sw.ToString(); } Console.WriteLine(xml); using (StringReader sr = new StringReader(xml)) { Person p = (Person)serializer.Deserialize(sr); Console.WriteLine("{0} {1} is {2} years old", p.FirstName, p.LastName, p.Age); } Console.ReadLine(); }
static void Main(string[] args) { Person p = new Person { Id = 3, Name = "John Doe JSON" }; using (MemoryStream stream = new MemoryStream()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person)); ser.WriteObject(stream, p); stream.Position = 0; StreamReader streamReader = new StreamReader(stream); Console.WriteLine(streamReader.ReadToEnd()); stream.Position = 0; Person result = (Person)ser.ReadObject(stream); Console.WriteLine("Memory: {0} {1}", result.Id, result.Name); } using (Stream stream = new FileStream("data.json", FileMode.Create)) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person)); ser.WriteObject(stream, p); } using (Stream stream = new FileStream("data.json", FileMode.Open)) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person)); Person result = (Person)ser.ReadObject(stream); Console.WriteLine("File: {0} {1}", result.Id, result.Name); } Console.ReadLine(); }
static void Main(string[] args) { Person p = new Person { Id = 2, Name = "John Doe" }; using (Stream stream = new FileStream("data.xml", FileMode.Create)) { DataContractSerializer ser = new DataContractSerializer(typeof(Person)); ser.WriteObject(stream, p); } using (Stream stream = new FileStream("data.xml", FileMode.Open)) { DataContractSerializer ser = new DataContractSerializer(typeof(Person)); Person result = (Person)ser.ReadObject(stream); Console.WriteLine("{0} {1}", result.Id, result.Name); } Console.ReadLine(); }
static void Main(string[] args) { Person p = new Person { Id = 1, Name = "John Doe", }; IFormatter formatter = new BinaryFormatter(); using (Stream stream = new FileStream("data.bin", FileMode.Create)) { formatter.Serialize(stream, p); } using (Stream stream = new FileStream("data.bin", FileMode.Open)) { Person dp = (Person)formatter.Deserialize(stream); Console.WriteLine("{0} {1}", dp.Id, dp.Name); } Console.ReadLine(); }
static void Main(string[] args) { int x = 1; int y = 0; try { int result = x / y; } catch (DivideByZeroException) when(y == 0 && x == 0) { Console.WriteLine("y не должен быть равен 0"); } catch (DivideByZeroException) when(y == 0) { Console.WriteLine("y не должен быть равен 0!!!"); } catch (DivideByZeroException ex) { Console.WriteLine(ex.Message); } try { int x1 = 5; int y1 = x1 / 0; Console.WriteLine($"Результат: {y1}"); } catch (Exception ex) { Console.WriteLine($"Исключение: {ex.Message}"); Console.WriteLine($"Метод: {ex.TargetSite}"); Console.WriteLine($"Трассировка стека: {ex.StackTrace}"); } try { object obj = "you"; int num = (int)obj; // InvalidCastException Console.WriteLine($"Результат: {num}"); } catch (DivideByZeroException) { Console.WriteLine("Возникло исключение DivideByZeroException"); } catch (IndexOutOfRangeException) { Console.WriteLine("Возникло исключение IndexOutOfRangeException"); } catch (Exception ex) { Console.WriteLine($"Исключение: {ex.Message}"); } try { Person p = new Person("Tom", 17); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } try { Human h = new Human { Name = "Tom", Age = 13 }; } catch (MyExceptions ex) { Console.WriteLine($"Error: {ex.Message}"); Console.WriteLine($"Incorrect value: {ex.Value}"); } Console.WriteLine("/n/n"); try { TestClass.Method1(); } catch (DivideByZeroException ex) { Console.WriteLine($"Catch в Main : {ex.Message}"); } finally { Console.WriteLine("Блок finally в Main"); } Console.WriteLine("Конец метода Main"); Console.Read(); try { Console.Write("Введите строку: "); string message = Console.ReadLine(); if (message.Length > 6) { throw new Exception("Длина строки больше 6 символов"); } } catch { Console.WriteLine("Возникло исключение"); throw; } }