Supporting Material for ‘.NET and Linux’ tech talk
Code Snippets
Unguarded Code
var registryValue =
.GetValue("HKEY_CURRENT_USER", "value", "blarg");
Registry
.WriteLine(registryValue); Console
This code will raise a type initializer exception if run on a non-Windows system.
It will also generate a compile-time warning: “warning CA1416: This call site is reachable on all platforms. ‘Registry.GetValue(string, string?, object?)’ is only supported on: ‘windows’”
Guarded Code
var registryValue = (OperatingSystem.IsWindows())
? Registry.GetValue("HKEY_CURRENT_USER", "value", "blarg")
: $"Registry does not exist in {Environment.OSVersion}";
.WriteLine(registryValue); Console
This code will run successfully on all platforms. It will not generate a compile-time warning, as the compiler will see that the code is guarded.
Simple IoT Example
This is a simple code example for blinking an LED on a breakout board attached to a Raspberry Pi.
using System;
using System.Device.Gpio;
using System.Threading;
.WriteLine("Blinking LED. Press Ctrl+C to end.");
Consoleint pin = 18;
using var controller = new GpioController();
.OpenPin(pin, PinMode.Output);
controllerbool ledOn = true;
while (true)
{
.Write(pin, ((ledOn) ? PinValue.High : PinValue.Low));
controller.Sleep(1000);
Thread= !ledOn;
ledOn }
Full example is here.
Links
Download .NET - Downloads for .NET, including ASP.NET Core.
Install .NET on Linux Distributions
.NET Runtime Identifier (RID) catalog
.NET IoT - Develop apps for IoT devices with the .NET IoT Libraries.
Writing cross platform P/Invoke code
Language Comparison - Go
go.dev - Go home page
Go by Example - Annotated example programs.
Go GOOS and GOARCH - Platform targeting values.
Language Comparison - Rust
Rust Programming Language - Rust home page
Rust by Example - Collection of runnable example programs illustrating various Rust concepts.
Rust Cookbook - Collection of simple examples that demonstrate good practices to accomplish common programming tasks.