cs11dotnet7/notebooks/Chapter07.dib
2022-02-09 10:30:33 +00:00

81 lines
1.6 KiB
Plaintext

#!markdown
# Chapter 7 - Packaging and Distributing .NET Types
Most of this chapter is about creating apps and class libraries for deployment so .NET Interactive notebooks cannot be used for most code examples.
#!markdown
## Importing a namespace to use a type
#!csharp
using System.Xml.Linq; // XDocument
#!csharp
XDocument doc = new();
#!markdown
## Relating C# keywords to .NET types
One of the common questions I get from new C# programmers is, "What is the difference
between `string` with a lowercase s and `String` with an uppercase S?"
#!csharp
using System; // String
#!csharp
string s1 = "Hello";
String s2 = "World";
#!markdown
## Understanding native-sized integers
C# 9 introduced `nint` and `nuint` keyword alias for native-sized integers, meaning that the
storage size for the integer value is platform specific.
#!csharp
using static System.Console;
#!csharp
WriteLine($"int.MaxValue = {int.MaxValue:N0}");
WriteLine($"nint.MaxValue = {nint.MaxValue:N0}");
#!markdown
## Testing your class library package
#!csharp
#r "nuget:packt.csdotnet.sharedlibrary,6.0.0"
#!csharp
using Packt.Shared;
#!csharp
#nullable enable
Write("Enter a color value in hex: ");
string? hex = "00ffc8"; // or ReadLine();
WriteLine("Is {0} a valid color value? {1}",
arg0: hex, arg1: hex.IsValidHex());
Write("Enter a XML element: ");
string? xmlTag = "<h1 class=\"<\" />"; // or ReadLine()
WriteLine("Is {0} a valid XML element? {1}",
arg0: xmlTag, arg1: xmlTag.IsValidXmlTag());
Write("Enter a password: ");
string? password = "secretsauce"; // or ReadLine()
WriteLine("Is {0} a valid password? {1}",
arg0: password, arg1: password.IsValidPassword());