How to resolve the algorithm Object serialization step by step in the C# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Object serialization step by step in the C# programming language

Table of Contents

Problem Statement

Create a set of data types based upon inheritance. Each data type or class should have a print command that displays the contents of an instance of that class to standard output. Create instances of each class in your inheritance hierarchy and display them to standard output. Write each of the objects to a file named objects.dat in binary form using serialization or marshalling. Read the file objects.dat and print the contents of each serialized object.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Object serialization step by step in the C# programming language

The code snippet is written in C# and it demonstrates object serialization and deserialization. It defines two classes: Being and Animal, both of which are marked with the [Serializable] attribute. The Being class has a single property, Alive, which indicates whether the being is alive or not. The Animal class inherits from the Being class and adds two additional properties: Id and Name. The Animal class also has a constructor that takes three parameters: id, name, and alive. The constructor initializes the Id, Name, and Alive properties of the object.

The Program class is the entry point of the program. It defines a list of Animal objects and then iterates through the list, printing the Name, Id, and Alive properties of each object. The program then serializes the list of Animal objects to a file named objects.dat using the BinaryFormatter class. The program then clears the list of Animal objects and deserializes the list of Animal objects from the file objects.dat using the BinaryFormatter class. The program then iterates through the deserialized list of Animal objects, printing the Name, Id, and Alive properties of each object.

The output of the program is as follows:

Fido, id=1 is alive
Lupo, id=2 is alive
Wanda, id=7 is alive
Kiki, id=3 is dead
---------------
Fido, id=1 is alive
Lupo, id=2 is alive
Wanda, id=7 is alive
Kiki, id=3 is dead

Source code in the csharp programming language

using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;

namespace Object_serialization
{
  [Serializable] public class Being
  {
    public bool Alive { get; set; }
  }

  [Serializable] public class Animal: Being
  {
    public Animal() { }

    public Animal(long id, string name, bool alive = true)
    {
      Id = id;
      Name = name;
      Alive = alive;
    }

    public long Id { get; set; }
    public string Name { get; set; }

    public void Print() { Console.WriteLine("{0}, id={1} is {2}",
      Name, Id, Alive ? "alive" : "dead"); }
  }


  internal class Program
  {
    private static void Main()
    {
      string path = 
        Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\objects.dat";

      var n = new List<Animal>
              {
                new Animal(1, "Fido"),
                new Animal(2, "Lupo"),
                new Animal(7, "Wanda"),
                new Animal(3, "Kiki", alive: false)
              };

      foreach(Animal animal in n)
        animal.Print();

      using(var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
        new BinaryFormatter().Serialize(stream, n);

      n.Clear();
      Console.WriteLine("---------------");
      List<Animal> m;

      using(var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        m = (List<Animal>) new BinaryFormatter().Deserialize(stream);

      foreach(Animal animal in m)
        animal.Print();
    }
  }
}


  

You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the Rust programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Swift programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Fortran programming language
You may also check:How to resolve the algorithm Unbias a random generator step by step in the GAP programming language
You may also check:How to resolve the algorithm Numbers which are not the sum of distinct squares step by step in the Wren programming language