- 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 Web開發(fā)學(xué)習(xí)手冊(cè)
- 解構(gòu)產(chǎn)品經(jīng)理:互聯(lián)網(wǎng)產(chǎn)品策劃入門寶典
- Python快樂編程:人工智能深度學(xué)習(xí)基礎(chǔ)
- LabVIEW2018中文版 虛擬儀器程序設(shè)計(jì)自學(xué)手冊(cè)
- JavaScript高效圖形編程
- 華為HMS生態(tài)與應(yīng)用開發(fā)實(shí)戰(zhàn)
- PyTorch Artificial Intelligence Fundamentals
- Django Design Patterns and Best Practices
- 精通軟件性能測(cè)試與LoadRunner實(shí)戰(zhàn)(第2版)
- 編寫高質(zhì)量代碼:改善C程序代碼的125個(gè)建議
- Big Data Analytics
- Apache Mahout Clustering Designs
- 數(shù)據(jù)結(jié)構(gòu)習(xí)題解析與實(shí)驗(yàn)指導(dǎo)
- Creating Stunning Dashboards with QlikView
- Python預(yù)測(cè)分析實(shí)戰(zhàn)