using Newtonsoft.Json;
using System;
/// <summary>
/// Demonstration of new .NET 5 feature with C# 9.0
/// Also includes information on how to build and run on Linux.
/// See linked http pages for reference.
/// </summary>
// https://www.youtube.com/watch?v=cvOjkQtK3qU
// https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu
// https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file
// https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9
namespace LinuxMacOS
{
public static class Program
{
// immutable records with inheritence
public abstract record Person(string FirstName, string LastName)
{
public int Age { get; init; }
public string[] PhoneNumbers { get; init; }
}
public record Teacher(string FirstName, string LastName, int Grade)
: Person(FirstName, LastName);
public record Student(string FirstName, string LastName, int Grade)
: Person(FirstName, LastName);
// pattern matching
public static bool IsLetter(this char c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';
public static bool IsLetterOrSeparator(this char c) => c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or '.' or ',';
static void Main(string[] args)
{
Console.WriteLine("Hello World! This should run on Linux.");
Console.WriteLine("This is demonstration of .NET 5 C# 9.0 features.");
Console.WriteLine();
var person1 = new Teacher("Nancy", "Davolio", 10) { Age = 50 };
Console.WriteLine(person1);
// known object with new
Student person2 = new("Tim", "Hsu", 1) { Age = 30 };
Console.WriteLine(person2);
Console.WriteLine();
// Newtonsoft Serialize
string json = JsonConvert.SerializeObject(person2, Formatting.Indented);
if (json is not null) // check not null
{
Console.WriteLine(json);
}
}
}
}