O'Reilly logo

C# 7 and .NET Core Cookbook by Dirk Strauss

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. In the Chapter1 class, create a new method called OutputInformation() that takes a person object as parameter.
        public void OutputInformation(object person)        {        }
  1. Inside this method, we would need to check what type of object is passed to it. Traditionally, we would need to do the following:
        if (person is Student)        {          Student student = (Student)person;          WriteLine($"Student {student.Name} {student.LastName}                    is enrolled for courses {String.Join<int>(                    ", ", student.CourseCodes)}");        }        if (person is Professor)        {          Professor prof = (Professor)person;          WriteLine($"Professor {prof.Name} {prof.LastName}                     teaches {String.Join<string>(",", prof.TeachesSubjects)}");        }
  1. We have two if statements. We are expecting either a Student object ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required