官术网_书友最值得收藏!

Getting ready

To illustrate the concept of pattern matching, assume the following scenario. We have two object types called Student and Professor. We want to minimize code, so we want to create a single method to output the data from the object passed to it. This object can be a Student or a Professor object. The method needs to figure out which object it is working with and act accordingly. But first, we need to do a few things inside our console application to set things up:

  1. Ensure that you have added the following using statement.
        using System.Collections.Generic;
  1. You now need to create two new classes called Student and Professor. The code for the Student class needs to look as follows:
        public class Student
{
public string Name { get; set; }
public string LastName { get; set; }
public List<int> CourseCodes { get; set; }
}
  1. Next, the code for the Professor class needs to look as follows:
        public class Professor
{
public string Name { get; set; }
public string LastName { get; set; }
public List<string> TeachesSubjects { get; set; }
}

To understand where we are going with pattern matching, we first need to understand where we have come from. I will start the next section off by showing you how developers might have written this code before C# 7.0.

主站蜘蛛池模板: 屯留县| 哈巴河县| 诏安县| 荥阳市| 高密市| 郑州市| 奉化市| 余干县| 东丰县| 安新县| 钦州市| 宜春市| 宁陕县| 万宁市| 敖汉旗| 会同县| 扬中市| 清苑县| 靖宇县| 枝江市| 乌鲁木齐县| 石狮市| 诸城市| 新宁县| 红原县| 望城县| 醴陵市| 泗洪县| 石狮市| 乌拉特中旗| 平舆县| 彝良县| 息烽县| 宜良县| 喜德县| 枣庄市| 乌审旗| 马尔康县| 佳木斯市| 唐海县| 溧阳市|