aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/AddressBook/SampleUsage.cs
diff options
context:
space:
mode:
Diffstat (limited to 'csharp/src/AddressBook/SampleUsage.cs')
-rw-r--r--csharp/src/AddressBook/SampleUsage.cs45
1 files changed, 20 insertions, 25 deletions
diff --git a/csharp/src/AddressBook/SampleUsage.cs b/csharp/src/AddressBook/SampleUsage.cs
index 084b1655..c06188b4 100644
--- a/csharp/src/AddressBook/SampleUsage.cs
+++ b/csharp/src/AddressBook/SampleUsage.cs
@@ -1,4 +1,5 @@
-using System;
+using Google.Protobuf;
+using System;
using System.IO;
namespace Google.ProtocolBuffers.Examples.AddressBook
@@ -8,37 +9,31 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
private static void Main()
{
byte[] bytes;
- //Create a builder to start building a message
- Person.Builder newContact = Person.CreateBuilder();
- //Set the primitive properties
- newContact.SetId(1)
- .SetName("Foo")
- .SetEmail("foo@bar");
- //Now add an item to a list (repeating) field
- newContact.AddPhone(
- //Create the child message inline
- Person.Types.PhoneNumber.CreateBuilder().SetNumber("555-1212").Build()
- );
- //Now build the final message:
- Person person = newContact.Build();
- //The builder is no longer valid (at least not now, scheduled for 2.4):
- newContact = null;
+ // Create a new person
+ Person person = new Person
+ {
+ Id = 1,
+ Name = "Foo",
+ Email = "foo@bar",
+ Phone = { new Person.Types.PhoneNumber { Number = "555-1212" } }
+ };
using (MemoryStream stream = new MemoryStream())
{
- //Save the person to a stream
+ // Save the person to a stream
person.WriteTo(stream);
bytes = stream.ToArray();
}
- //Create another builder, merge the byte[], and build the message:
- Person copy = Person.CreateBuilder().MergeFrom(bytes).Build();
+ Person copy = Person.Parser.ParseFrom(bytes);
- //A more streamlined approach might look like this:
- bytes = AddressBook.CreateBuilder().AddPerson(copy).Build().ToByteArray();
- //And read the address book back again
- AddressBook restored = AddressBook.CreateBuilder().MergeFrom(bytes).Build();
- //The message performs a deep-comparison on equality:
- if (restored.PersonCount != 1 || !person.Equals(restored.PersonList[0]))
+ // A more streamlined approach might look like this:
+ bytes = copy.ToByteArray();
+ // And read the address book back again
+ AddressBook restored = AddressBook.Parser.ParseFrom(bytes);
+ // The message performs a deep-comparison on equality:
+ if (restored.Person.Count != 1 || !person.Equals(restored.Person[0]))
+ {
throw new ApplicationException("There is a bad person in here!");
+ }
}
}
} \ No newline at end of file