.NET Core – Create .NET Standard Library
”;
A class library defines the types and methods that can be called from any application.
-
A class library developed using .NET Core supports the .NET Standard Library, which allows your library to be called by any .NET platform that supports that version of the .NET Standard Library.
-
When you finish your class library, you can decide whether you want to distribute it as a third-party component, or whether you want to include it as a component that is bundled with one or more applications.
Let us start by adding a class library project in our Console application; right-click on the src folder in Solution Explorer and select Add → New Project…
In the Add New Project dialog box, choose the .NET Core node, then choose the Class Library (.NET Core) project template.
In the Name text box, enter “UtilityLibrary” as the name of the project, as the following figure shows.
Click OK to create the class library project. Once the project is created, let us add a new class. Right-click on project in Solution Explorer and select Add → Class…
Select class in the middle pane and enter StringLib.cs in the name and field and then click Add. Once the class is added, then replace the following code in StringLib.cs file.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace UtilityLibrary { public static class StringLib { public static bool StartsWithUpper(this String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsUpper(ch); } public static bool StartsWithLower(this String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsLower(ch); } public static bool StartsWithNumber(this String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsNumber(ch); } } }
-
The class library, UtilityLibrary.StringLib, contains some methods like, StartsWithUpper, StartsWithLower, and StartsWithNumber which returns a Boolean value that indicates whether the current string instance begins with an uppercase, lowercase and number respectively.
-
In .NET Core, the Char.IsUpper method returns true if a character is in uppercase, the Char.IsLower method returns true if a character is in lowercase, and similarly the Char.IsNumber method returns true if a character is a numeric.
-
On the menu bar, choose Build, Build Solution. The project should compile without error.
-
Our .NET Core console project doesn”t have access to our class library.
-
Now to consume this class library we need to add reference of this class library in our console project.
To do so, expand FirstApp and right-click on References and select Add Reference…
In the Reference Manager dialog box, select UtilityLibrary, our class library project, and then click OK.
Let us now open the Program.cs file of the console project and replace all of the code with the following code.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using UtilityLibrary; namespace FirstApp { public class Program { public static void Main(string[] args) { int rows = Console.WindowHeight; Console.Clear(); do { if (Console.CursorTop >= rows || Console.CursorTop == 0) { Console.Clear(); Console.WriteLine("nPress <Enter> only to exit; otherwise, enter a string and press <Enter>:n"); } string input = Console.ReadLine(); if (String.IsNullOrEmpty(input)) break; Console.WriteLine("Input: {0} {1,30}: {2}n", input, "Begins with uppercase? ", input.StartsWithUpper() ? "Yes" : "No"); } while (true); } } }
Let us now run your application and you will see the following output.
For better understanding, let us make use of the other extension methods of your class library in your project.
”;