- C# 7 and .NET Core Cookbook
- Dirk Strauss
- 224字
- 2021-07-03 00:11:51
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:
- Ensure that you have added the following using statement.
using System.Collections.Generic;
- 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; }
}
- 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.
推薦閱讀
- Java程序設計(慕課版)
- TypeScript Blueprints
- Clojure for Domain:specific Languages
- 征服RIA
- 深度學習:算法入門與Keras編程實踐
- 自然語言處理Python進階
- 智能手機故障檢測與維修從入門到精通
- 奔跑吧 Linux內核
- Julia High Performance(Second Edition)
- C語言程序設計
- Mobile Forensics:Advanced Investigative Strategies
- Spring Boot從入門到實戰
- Implementing Domain:Specific Languages with Xtext and Xtend
- 計算機常用算法與程序設計教程(第2版)
- Getting Started with Hazelcast