public void Remove(Person person) { List.Remove(person); }
public void Insert(int index, Person person) { List.Insert(index, person); }
public void Add(Person person) { List.Add(person); }
private void Array_Method_Clone_Sample() { // concept of shallow copy in here, using the Clone method. Person[] vetPerson = new Person[2]; vetPerson[0] = new Person() { Id = 1, BirthDate = DateTime.Now, Name = "Me" }; vetPerson[1] = new Person() { Id = 2, BirthDate = DateTime.Now, Name = "You" }; Person[] clonedVetPerson = (Person[])vetPerson.Clone(); clonedVetPerson[0].Name = "Me2"; // MessageBox.Show(vetPerson[0].Name + Resources.FrmChapter9_ArrayList_Clear_Sample____ + clonedVetPerson[0].Name); // The Name Me2 will be shown in here. // a different situation. If you replace an instace in the cloned array, this is what happens: Person[] vetPerson2 = new Person[2]; vetPerson2[0] = new Person() { Id = 1, BirthDate = DateTime.Now, Name = "Me2" }; vetPerson2[1] = new Person() { Id = 2, BirthDate = DateTime.Now, Name = "You2" }; Person[] clonedVetPerson2 = (Person[])clonedVetPerson.Clone(); clonedVetPerson2[0] = new Person() { Id = 1, BirthDate = DateTime.Now, Name = "Me3" }; MessageBox.Show(vetPerson2[0].Name + " - " + clonedVetPerson2[0].Name); // The Name Me2 will be shown in here. // Notice that this time the names are different because in the cloned array the reference // in the first element was replaced, but it didn’t replace the reference in the first array. }
private void btnXmlSerialization_Click(object sender, EventArgs e) { // * You use the XmlSerializer class in the System.Xml.Serialization namespace. // * One difference between the XmlSerializer and BinaryFormatter is that the XmlSerializer serializes only public properties and fields. // * You also do not need to use the[Serializable] attribute when declaring the class. // * Also, the class must be public. var person = new Person(); person.FirstName = "Cassio"; person.LastName = "Vinicius"; person.SetId(1); var xmlSerializerWriter = new XmlSerializer(typeof(Person)); var streamWriter = new StreamWriter("Person.xml"); xmlSerializerWriter.Serialize(streamWriter, person); streamWriter.Close(); var xmlSerializerReader = new XmlSerializer(typeof(Person)); var fileStream = new FileStream("Person.xml", FileMode.Open); var personRead = (Person)xmlSerializerReader.Deserialize(fileStream); fileStream.Close(); //var xmlSerializer = new XmlSerializer(typeof(Person)); //var streamWriter = new StreamWriter("Person.xml"); //xmlSerializer.Serialize(streamWriter, person); //streamWriter.Close(); ////The code produces the following file: ////<? xml version="1.0" encoding="utf-8"?> ////<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" //// xmlns:xsd="http://www.w3.org/2001/XMLSchema"> //// <FirstName>Joe</FirstName> //// <LastName>Smith</LastName> ////</Person> //// If you want to ignore a property, use the[XmlIgnore] attribute before the property. The following ////code would ignore the FirstName property when serializing the object to XML: ////[XmlIgnore] ////public string FirstName; //// You can use the following code to read the XML back into an object: //XmlSerializer xmlSerializer2 = new XmlSerializer(typeof(Person)); //FileStream fs = new FileStream("Person.xml", FileMode.Open); //Person person2 = (Person)xmlSerializer.Deserialize(fs); //fs.Close(); }
private void BinarySerialization_Overview() { // * The BinaryFormatter object is used to serialize and deserialize an object. // * This is found in the System.Runtime.Serialization.Formatters.Binary namespace. // * The two main methods you need to be concerned about are the Serialize and Desearialize methods. // * You can use the BinaryFormatter along with a FileStream to read and write your objects to disk. // * Remember, the FileStream is an object used to read and write data to and from disk as byte arrays. // * For a class to be serialized, you must add the[Serializable] attribute to the top of the class. // * The following example creates a class called Person and makes it serializable: // [Serializable] // class Person // { // private int _id; // public string FirstName; // public string LastName; // public void SetId(int id) // { // _id = id; // } // } // If you want to persist this object’s data to storage, you can create an instance of the BinaryFormatter // object and call its Serialize method. var person = new Person(); person.SetId(1); person.FirstName = "Joe"; person.LastName = "Smith"; IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream("Person.bin", FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, person); stream.Close(); // Be aware that even the private field will be persisted to the disk.You can restore the state of the // object by reading the Person.bin file and deserializing the file. stream = new FileStream("Person.bin", FileMode.Open, FileAccess.Read, FileShare.Read); var person2 = (Person)formatter.Deserialize(stream); stream.Close(); //If you execute the code and view the person2 object in the Watch window, you can see that all the //fields retain their value, even the private _id field.If you want to prevent the private field from being //persisted, you can add the[NonSerialized] attribute before the field declaration. //[Serializable] //class Person // { // [NonSerialized] // private int _id; // public string FirstName; // public string LastName; // public void SetId(int id) // { // _id = id; // } // } // When serializing the object, the _id field will be skipped.If you were to run the code again and // serialize the object and deserialize the object and view person2 in the Watch window, you would // notice the _id is 0. }