====== Supporting Material for ".NET and Linux" tech talk ====== ===== Code Snippets ===== ==== Unguarded Code ==== var registryValue = Registry.GetValue("HKEY_CURRENT_USER", "value", "blarg"); Console.WriteLine(registryValue); 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}"; Console.WriteLine(registryValue); 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; Console.WriteLine("Blinking LED. Press Ctrl+C to end."); int pin = 18; using var controller = new GpioController(); controller.OpenPin(pin, PinMode.Output); bool ledOn = true; while (true) { controller.Write(pin, ((ledOn) ? PinValue.High : PinValue.Low)); Thread.Sleep(1000); ledOn = !ledOn; } Full example is [[https://docs.microsoft.com/en-us/dotnet/iot/tutorials/blink-led|here]]. ===== Links ===== [[https://dotnet.microsoft.com/en-us/download|Download .NET]] – Downloads for .NET, including ASP.NET Core. [[https://docs.microsoft.com/en-us/dotnet/core/install/linux|Install .NET on Linux Distributions]] [[https://docs.microsoft.com/en-us/dotnet/core/rid-catalog|.NET Runtime Identifier (RID) catalog]] [[https://docs.microsoft.com/en-us/dotnet/iot/intro|.NET IoT]] – Develop apps for IoT devices with the .NET IoT Libraries. [[https://docs.microsoft.com/en-us/dotnet/standard/native-interop/cross-platform|Writing cross platform P/Invoke code]] ==== Language Comparison – Go ==== [[https://go.dev/|go.dev]] – Go home page [[https://go.dev/doc/tutorial/getting-started|Tutorial: Get Started with Go]] [[https://gobyexample.com/|Go by Example]] – Annotated example programs. [[https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63|Go GOOS and GOARCH]] – Platform targeting values. ==== Language Comparison – Rust ==== [[https://www.rust-lang.org/|Rust Programming Language]] – Rust home page [[https://doc.rust-lang.org/stable/rust-by-example/|Rust by Example]] – Collection of runnable example programs illustrating various Rust concepts. [[https://rust-lang-nursery.github.io/rust-cookbook/|Rust Cookbook]] – Collection of simple examples that demonstrate good practices to accomplish common programming tasks. [[https://doc.rust-lang.org/nightly/rustc/platform-support.html|Platform Support]]