How to do it...

  1. Start by adding an abstract class called Cat. This class simply defines fields for Weight and Age. Note that in order to make your class serializable, you need to add the [Serializable] attribute to it.
        [Serializable]        public abstract class Cat        {          // fields          public int Weight;          public int Age;         }
  1. Next, create a class called Tiger that is derived from the Cat class. Note that the Tiger class must also have the [Serializable] attribute added. This is because the serialization isn't inherited from the base class. Each derived class must implement serialization on its own:
        [Serializable]        public class Tiger : Cat        {          public string Trainer;          public bool IsTamed;        }
  1. Next, we need to create a method to serialize the Tiger class. ...

Get C# 7 and .NET Core Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.