.NET Standard 2.0 Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. Open Visual Studio 2017.
  2. 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.
  3. In the Name: text box, type Chapter2.Collections 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 is they are:



  1. Click OK
  2. Now, in the Solution Explorer (or press Ctrl + Alt + L), select Chapter2.Collections. Right-click and select Add | New Project.
  3. In the Add New Project dialog box, expand the Visual C# node and select .NET Standard in the left-hand pane. 
  1. In the right-hand pane, select Class Library (.NET Standard):
  1. Now, in the Name: text box type Chapter2.Collections.CollectionsLib and leave the Location: text box, as it is:
  1. Click OK.
  1. Now, the Solution Explorer should look like this:
  1. Click on the Class1.cs and press F2 to rename it. Type LittleShop.cs as the new name. 
  2. Select Yes in the confirmation dialog box for renaming. 
  3. Now double-click on LittleShop.cs to open its code window. 
  4. At the top of the code window, move the cursor (or click the mouse) at the last line of the using directives and add the following using directive: 
      using System.Collections.Generic;
  1. Type the following code in between the curly brackets of the LittleShop class: 
      public List<string> GetFruitsList()
{
var fruitsList = new List<string>();

fruitsList.Add("Apples");
fruitsList.Add("Grapes");
fruitsList.Add("Mangoes");
fruitsList.Add("Oranges");
fruitsList.Add("Pineapples");

return fruitsList;
}
  1. Let's press Ctrl + Shift + B for a quick build and check for any syntax errors. 
  2. Now type the following code at the end of the using directives at the top of your code window: 
      using System.Collections;
  1.  At the end of the GetFruitsList() method, add the following code: 
      public ArrayList GetShopItems()
{
var shopItems = new ArrayList();
shopItems.Add("Fruits");
shopItems.Add("Vegetables");
shopItems.Add("Chocolates");

return shopItems;
}
  1. Let's quickly hit a quick Ctrl + B to debug and check for syntax errors. 
  2. Now we are good to go and test the library.