- .NET Core - Migrations
- Restoring and Building & MSBuild
- .NET Core - MSBuild & project.json
- .NET Core - SDK
- Managed Extensibility Framework
- .NET Core - Testing Library
- Running Tests in Visual Studio
- .NET Core - Create a Testing Project
- .NET Core - PCL Troubleshooting
- Creating a Xamarin.Forms Project
- Sharing .NET Core Libraries
- Adding References to Library
- .NET Core - Portable Class Library
- Create .NET Standard Library
- Windows Runtime & Extension SDKs
- .NET Core - Metapackage
- .NET Core - MSBuild
- Create UWP App with .NET Core
- .NET Core - Package References
- .NET Core - Project Files
- .NET Core - Modularity
- .NET Core - Code Execution
- .NET Core - Garbage Collection
- .NET Core - Numerics
- .NET Core - Getting Started
- .NET Core - Environment Setup
- .NET Core - Prerequisites
- .NET Core - Overview
- .NET Core - Home
.NET Core Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
.NET Core - Create .NET Standard Library
A class pbrary defines the types and methods that can be called from any apppcation.
A class pbrary developed using .NET Core supports the .NET Standard Library, which allows your pbrary to be called by any .NET platform that supports that version of the .NET Standard Library.
When you finish your class pbrary, 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 apppcations.
Let us start by adding a class pbrary project in our Console apppcation; right-cpck 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 "UtiptyLibrary" as the name of the project, as the following figure shows.
Cpck OK to create the class pbrary project. Once the project is created, let us add a new class. Right-cpck 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 cpck 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 UtiptyLibrary { pubpc static class StringLib { pubpc static bool StartsWithUpper(this String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsUpper(ch); } pubpc static bool StartsWithLower(this String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsLower(ch); } pubpc static bool StartsWithNumber(this String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsNumber(ch); } } }
The class pbrary, UtiptyLibrary.StringLib, contains some methods pke, 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 pbrary.
Now to consume this class pbrary we need to add reference of this class pbrary in our console project.
To do so, expand FirstApp and right-cpck on References and select Add Reference…
In the Reference Manager dialog box, select UtiptyLibrary, our class pbrary project, and then cpck 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 UtiptyLibrary; namespace FirstApp { pubpc class Program { pubpc static void Main(string[] args) { int rows = Console.WindowHeight; Console.Clear(); do { if (Console.CursorTop >= rows || Console.CursorTop == 0) { Console.Clear(); Console.WriteLine(" Press <Enter> only to exit; otherwise, enter a string and press <Enter>: "); } string input = Console.ReadLine(); if (String.IsNullOrEmpty(input)) break; Console.WriteLine("Input: {0} {1,30}: {2} ", input, "Begins with uppercase? ", input.StartsWithUpper() ? "Yes" : "No"); } while (true); } } }
Let us now run your apppcation and you will see the following output.
For better understanding, let us make use of the other extension methods of your class pbrary in your project.
Advertisements