• C# 12 features: Primary constructors

    Last month, we had a ".NET 8 Lightning talks" theme for the Adelaide .NET User Group. John covered Blazor, Ryan summarised a bunch of new core library features, and I focused on the 8 new language features added in C# 12. What a great idea for a nice, short blog series!

    C# logo

    Primary constructors

    C# has had primary constructors for records, but now we can use them for classes and structs!

    The parameters can be used anywhere within the class - methods, local functions as well as initialising fields and properties.

    A primary constructor guarantees that the named parameters have been supplied when an instance is created. You can optionally add other conventional constructors, but they must all call back to the primary constructor using the this(...) syntax.

    public class PrimaryConstructorClass(string name)
    {
        private string Name { get; init; } = name;
    
        private string Name2 => name;
    
        public void Thing()
        {
            Console.WriteLine(name);
        }
    }
    

    I've made regular use of these whenever I've been working on a .NET 8 project. I've found them very useful.

    Further reading

    https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/instance-constructors?WT.mc_id=DOP-MVP-5001655#primary-constructors

    Example source

    https://github.com/flcdrg/csharp-12/tree/main/01-primary-constructors

  • Which Western Digital Red NAS hard disk should I buy? (2024 update)

    Back in 2021, I first reviewed the options for Western Digital Red 4TB NAS hard disks. My Synology DS1621xs+ NAS has recently started warning me that storage space is getting low, so let's review the prices (and also consider the larger capacities) currently available from Western Digital in 2024.

    Western Digital WD Red Plus 10TB hard drive

    Western Digital differentiates its Red NAS drives into three groups/recommended workloads: Red (lighter SOHO), Red Plus (write-intensive), and Red Pro (highest-intensity).

    Model number Product Number Capacity (TB) Internal rate (MB/s) Cache (MB) RPM Recording Technology Approx. Date Spec sheet Price (AUD) Price (USD)
    Red WD40EFAX 4 180 256 5400 SMR Aug-20 Link 230 n/a
    Red Plus WD40EFZX 4 175 128 5400 CMR Jan-21 Link 257 111
    Red Plus WD40EFPX 4 180 256 5400 CMR Sep-22 Link 202 n/a
    Red Plus WD60EFPX 6 180 256 5400 CMR Sep-22 Link 289 n/a
    Red Plus WD80EFZZ 8 185 128 5640 CMR Jan-22 Link 313 n/a
    Red Plus WD80EFPX 8 215 256 5640 CMR Nov-23 Link 308 179
    Red Plus WD101EFBX 10 215 256 7200 CMR Jan-20 Link 398 199
    Red Plus WD120EFBX 12 196 256 7200 CMR Jan-21 Link 515 n/a
    Red Plus WD140EFGX 14 210 512 7200 CMR Jan-20 Link 658 n/a
    Red Pro WD4003FFBX 4 217 256 7200 CMR Sep-20 Link 212 n/a
    Red Pro WD6003FFBX 6 238 256 7200 CMR Feb-18 Link 339 204
    Red Pro WD8003FFBX 8 235 256 7200 CMR Apr-18 Link 404 229
    Red Pro WD102KFBX 10 256 256 7200 CMR Jan-20 Link 465 269
    Red Pro WD121KFBX 12 240 256 7200 CMR May-19 Link 491 285
    Red Pro WD161KFGX 16 259 256 7200 CMR Sep-20 Link 554 308
    Red Pro WD181KFGX 18 272 512 7200 CMR Sep-20 Link 583 342
    Red Pro WD221KFGX 22 256 512 7200 CMR Jul-22 Link 849 419

    Notes

    • Prices listed are from Amazon as of 25th February 2024. Click through the links to get the latest price, they seem to change daily!
    • The AUD price links to Amazon Australia and the US price links to Amazon.com. (Amazon links are affiliate links)
    • I live in Australia, hence the Amazon AU links, but sometimes the US prices are more competitive - it pays to compare both.
    • Dates are from the oldest specification sheets I've found for that model or data listed on the Amazon page.
    • Recording technology: SMR - Shingled Magnetic Recording, CMR - Conventional Magnetic Recording. More info

    My choice

    I've previously bought 4TB drives, but I'm thinking this time I might go for something larger. Because of the RAID configuration I am using, I'll only be effectively using 4TB of the new drive. But this is planning for the future, as when I buy a subsequent larger drive I'll then get access to the full capacity of both drives. The 'Red Plus' models look fine for my purposes. I don't need the extra features offered by the 'Red Pro' line.

    As I write this, the 10G Red Plus WD101EFBX has a discounted price on Amazon US (plus I have some Amazon US gift card credit waiting to be used), so that looks like a good deal. I was originally looking at the 8TB drives, but for only USD20 extra you get another 2TB, so why not?

    Table formatting

    As a side note, I collated the information in the table above using Excel. Initially, I was going to use a Visual Studio Code extension to paste the Excel content into a Markdown table format, but then formatting the links was not going to be easy. I then realised I could use PowerShell to manage the formatting if I saved the spreadsheet as a .CSV format. PowerShell automation for the win!

    eg.

    $data = (get-content 'C:\Users\david\OneDrive\Documents\Western Digital Hard disk costs.csv') | ConvertFrom-Csv
    $data | ForEach-Object { "| " + $_.'Model number' + " | " + $_.'Product Number' + " | " + $_.'Capacity (TB)' + " | " + $_.'Internal rate (MB/s)' + " | " + $_.'Cache (MB)' + " | " + $_.RPM + " | " + $_.'Recording Technology' + " | " + $_.'Approx. Date' + " | [Link](" + $_.'Spec sheet' + ") | [" + $_.'Price (AUD)' + "](" + $_.'Amazon AU'+ ") | [" + $_.'Price (USD)' + "](" + $_.'Amazon US' + ") |" } | clip
    

  • Laptop heat wave

    It hasn't all been smooth sailing with my new laptop. I had some stability issues last year where it was blue screening, and when it restarted the NVidia GPU was not present in the Device Manager (or Task Manager). After a few attempts with Dell Support replacing parts, it eventually started behaving again.

    Then a couple of weeks ago, I had a notification popup on my laptop. The Dell 'SupportAssist' app had noticed the fan had failed a couple of times recently. I was surprised, as I hadn't noticed any issues. I contacted Dell Support and they asked me to run the firmware-level SupportAssist diagnostic. Sure enough, it said there was an issue with a 'SKIN' sensor reading 'is lower than expected'. I'm not sure exactly what that is, but I did find a mention of the Error Code "2000-0315" on this page.

    Screenshot of SKIN sensor error message

    The warning turned out to be accurate as within a matter of days my laptop was quickly becoming unreliable.

    Screenshot of SupportAssist error message indicating a high system temperature caused system to be automatically powered off

    Dell arranged an onsite visit from a support technician, who ended up replacing fans and heat-sinks.

    Frustratingly, that very evening after the repair had been completed, I was using my laptop again, and this time it spontaneously shutdown with no warning. I was surprised as it didn't even feel particularly warm. Restarting it showed the following screen, so it obviously still had a problem.

    Screenshot of error 'Thermal trip detected' and system automatically powered off

    Dell organised another support visit, this time to replace the entire motherboard. When that technician arrived, he unpacked the replacement board only to discover it had been damaged. So now I'd have to wait for them to send out another replacement board. Initially, that was predicted to take almost two weeks (possibly they'd run out of stock in Australia?).

    But then early yesterday I got an email saying the tech was coming that morning. I was a bit surprised - usually they let you know the day before and you can always reschedule the appointment if it isn't convenient. I tried to do just that but their website just shows a generic error. Later that morning the time window was coming to an end when I received another email saying they'd bumped the time to later in the day. That could be problematic as I had an appointment I needed to attend late afternoon. Fortunately, the tech rang early afternoon and was able to swap over a new motherboard with plenty of time to spare.

    So hopefully we're now back to a working laptop.

    Not completely smooth sailing however. When they replace the motherboard, that includes a new Trusted Platform Module (TPM) chip - so things like your Windows Hello PIN, fingerprint and Face Recognition won't work and will need to be reset. But this time around, Windows wouldn't let me. I'd sign in, confirm on my phone with the Authenticator app, and then it would fail.

    Screenshot of Windows status - 'Something went wrong. We weren't able to set up your PIN. You can try again, or skip this step and set up a PIN later'

    I don't think there's a way around this error. Retry gave the same error and 'Skip for now' took you back to the sign-in screen.

    I did happen to have a second local account on the machine, but it isn't an admin account. I could sign in successfully with that account, but not switch to my Microsoft Account. I think I'm looking at a complete reinstall. I wasn't overly worried by this as I know anything important is already synced in OneDrive. I also make use of the Synology Active Business Backup to backup all our computers at home, so I was confident I'd also have a full backup image of the machine that I could get any files from if needed.

    Before paving the machine, I wisely checked the Synology backup status. I currently choose to only run the Synology on the weekends (to save power and $$), so that does also mean backups can only happen on the weekend. I was surprised to find that the last backup was from mid-January!

    Pleasingly, the Synology backup agent was already present on the laptop, and I could control that from the Synology management interface to initiate a fresh backup (rather than have to wait for the next scheduled one to start). If you go to the Task List panel, then you can right-click on the row for the computer in question and select Back up.

    Synology backup context menu

    Now with a fresh backup, I'm ready to reinstall Windows and run my Boxstarter scripts to install all the applications I need.

    I also noticed my Synology disks are getting a bit full, so it might be time to buy another, larger hard disk to ensure I have enough space going forward.