- .NET Standard 2.0 Cookbook
- Fiqri Ismail
- 475字
- 2021-06-25 22:11:40
How to do it...
- Open Visual Studio 2017.
- Click File | New | Project and, in the New Project template dialog box, select Visual Studio Solutions under the Other Project Types node in the left-hand pane and then Blank Solution in the right-hand pane.
- In the Name: text box, type Chapter2.Linq as the name of the solution. Select a preferred location under the Location: drop-down list or click the Browse... button and select a location. Leave the defaults as they are:
- Click OK.
- Now, in the Solution Explorer (or press Ctrl + Alt + L), select Chapter2.Linq. Right-click and select Add | New Project.
- In the Add New Project dialog box, expand the Visual C# node and select .NET Standard in the left-hand pane.
- In the right-hand pane, select Class Library (.NET Standard).

- Now, in the Name: text box, type Chapter2.Linq.QueriesLib and leave the Location: text box as it is:

- Click OK.
- Now, the Solution Explorer (press Ctrl + Alt + L to open) should look like this:

- Select Class1.cs in the project tree and press F2.
- Rename Class1.cs as TelephoneBook.cs, also making sure that you have done the same to the class name itself.
- Answer Yes in the confirmation dialog box for renaming.
- Now, the Solution Explorer should look like this:

- Double-click the TelephoneBook.cs file to open the code window.
- Now, scroll up to the top of the code window and add the following code next to the last line of the using directives:
using System.Collections.Generic;
using System.Linq;
- Again, scroll down till you reach the open curly bracket of the TelephoneBook class and add the following code:
private List<string> _contactList;
- Now, in the next line, add the following code as the constructor method:
public TelephoneBook()
{
_contactList = new List<string>();
_contactList.Add("Lenna Paprocki");
_contactList.Add("Donette Foller");
_contactList.Add("Simona Morasca");
_contactList.Add("Mitsue Tollner");
_contactList.Add("Leota Dilliard");
_contactList.Add("Sage Wieser");
_contactList.Add("Kris Marrier");
_contactList.Add("Minna Amigon");
_contactList.Add("Abel Maclead");
_contactList.Add("Kiley Caldarera");
_contactList.Add("Graciela Ruta");
}
- Next to the constructor, add the following code:
public List<string> GetContacts()
{
return _contactList;
}
- Again, add this code block at the end of the GetContacts() method:
public List<string> GetContactsByLastName(string lastName)
{
var contacts = _contactList.Where(
c => c.Contains(lastName)).ToList();
return contacts;
}
- Finally, add the following code block at the end of the GetContactsByLastName() method:
public List<string> GetSortedContacts(bool ascending = true)
{
var sorted = _contactList.OrderBy(c => c).ToList();
if (!ascending)
{
sorted = _contactList.OrderByDescending(c => c).ToList();
}
return sorted;
}
- Now that we are done with adding code to the .NET Standard 2.0 library, let's hit Ctrl + Shift + B for a quick build and check for syntax errors.
推薦閱讀
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- 精通JavaScript+jQuery:100%動(dòng)態(tài)網(wǎng)頁設(shè)計(jì)密碼
- Moodle Administration Essentials
- Raspberry Pi Networking Cookbook(Second Edition)
- 少年輕松趣編程:用Scratch創(chuàng)作自己的小游戲
- NumPy Essentials
- Visual C++串口通信技術(shù)詳解(第2版)
- 碼上行動(dòng):用ChatGPT學(xué)會(huì)Python編程
- Jupyter數(shù)據(jù)科學(xué)實(shí)戰(zhàn)
- 區(qū)塊鏈技術(shù)進(jìn)階與實(shí)戰(zhàn)(第2版)
- Citrix XenServer企業(yè)運(yùn)維實(shí)戰(zhàn)
- Visual Basic程序設(shè)計(jì)習(xí)題與上機(jī)實(shí)踐
- Django 5企業(yè)級(jí)Web應(yīng)用開發(fā)實(shí)戰(zhàn)(視頻教學(xué)版)
- Beginning C# 7 Hands-On:The Core Language
- Mastering Swift 4(Fourth Edition)