• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using System;
11 using System.IO;
12 
13 namespace Google.Protobuf.Examples.AddressBook
14 {
15     internal class SampleUsage
16     {
Main()17         private static void Main()
18         {
19             byte[] bytes;
20             // Create a new person
21             Person person = new Person
22             {
23                 Id = 1,
24                 Name = "Foo",
25                 Email = "foo@bar",
26                 Phones = { new Person.Types.PhoneNumber { Number = "555-1212" } }
27             };
28             using (MemoryStream stream = new MemoryStream())
29             {
30                 // Save the person to a stream
31                 person.WriteTo(stream);
32                 bytes = stream.ToArray();
33             }
34             Person copy = Person.Parser.ParseFrom(bytes);
35 
36             AddressBook book = new AddressBook
37             {
38                 People = { copy }
39             };
40             bytes = book.ToByteArray();
41             // And read the address book back again
42             AddressBook restored = AddressBook.Parser.ParseFrom(bytes);
43             // The message performs a deep-comparison on equality:
44             if (restored.People.Count != 1 || !person.Equals(restored.People[0]))
45             {
46                 throw new Exception("There is a bad person in here!");
47             }
48         }
49     }
50 }