Following the Microsoft tutorial for building a .Net Standard library with Visual Basic from here, I have decided to make a video for .Net Standard library with C#. Pretty much, it is one and the same.
The library contains one class and there is one method in the class, checking whether the first char of the string is an upper case or not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; namespace UtilityLibraries { public static class StringLibrary { public static bool StartsWithUpper(this String str) { if (String.IsNullOrWhiteSpace(str)) { return false; } Char ch = str[0]; return Char.IsUpper(ch); } } } |
Once the “library” is created, some tests are built as well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using UtilityLibraries; namespace StringLibraryTest { [TestClass] public class UnitTest1 { [TestMethod] public void TestStartsWithUpper() { string[] words = { "Alphabet", "Zulu", "ABC", "Витош", "ВитошАкадемия.ком" }; foreach (string word in words) { bool result = word.StartsWithUpper(); Assert.IsTrue(result, String.Format("Expected for '{0}': true; Actual {1}",word, result)); } } [TestMethod] public void TestDoesNotStartWithUpper() { string[] words = { "alphabet", "zulu", "aBC", "ш", "ком","55",".","" }; foreach (string word in words) { bool result = word.StartsWithUpper(); Assert.IsFalse(result, String.Format("Expected for '{0}': true; Actual {1}", word, result)); } } [TestMethod] public void TestWithNullOrEmpty() { string[] words = {string.Empty, null}; foreach (var word in words) { bool result = word.StartsWithUpper(); Assert.IsFalse(result, String.Format("Expected for '{0}': false; Actual {1}", word == null ? "" : word, result )); } } } } |
At the end, some show case is presented, showing the usage of the extension methods for the library. The code is a bit overcomplicated there, but this is how the tutorial looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using UtilityLibraries; class Startup { static void Main() { int row = 0; do { if (row == 0 || row >= 25) { ResetConsole(); } string input = Console.ReadLine(); if (String.IsNullOrEmpty(input)) break; Console.WriteLine($"Input: {input} {"Begins with uppercase? ",30}: " + $"{(input.StartsWithUpper() ? "Yes" : "No")}\n"); row += 3; } while (true); return; void ResetConsole() { if (row > 0) { Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } Console.Clear(); Console.WriteLine("\nPress only to exit; otherwise, enter a string and press :\n"); row = 3; } } } |
The whole code in GitHub is here. Enjoy it!