Struct in C#: Build a Value Type Step by Step (.NET 10)

Struct in C#: copying a struct leaves the original at 4, while copying a class changes the shared object to 16

Picture the shipping page of a Portland, Oregon, coffee roaster. Every bag ships in a carton, and each carton is just three numbers: length, width and height in inches. That’s the job of a struct in C#: a small value type that copies its data on assignment instead of sharing a reference. We’ll build that … Read more >>

C# Method Naming Conventions: PascalCase, Async and Try (.NET 10)

C# method naming conventions cheat sheet: CalculateTotal verb phrase, GetQuoteAsync suffix, TryFindPrice Try pattern and OnStockRanLow event method

Quick answer: C# method naming conventions say every method gets a PascalCase verb or verb phrase, like CalculateTotal, async methods end in Async, and parameters stay camelCase: I ran every rule below on .NET 10 against a gear store’s order code, then made the build flag the names you’d get wrong. What You Need Before … Read more >>

How to Send Email with Attachment in C# (MailKit and System.Net.Mail)

Send email with attachment in C#: the Attachments folder with Invoice-10482.pdf, PackingSlip-10482.pdf and DeliveryPhoto-10482.jpg sent with MailKit

To send email with attachment in C#, build the message with MailKit’s BodyBuilder, call builder.Attachments.Add() with a file path, a byte array or a stream, and send it with MailKit’s SmtpClient. Microsoft now advises against System.Net.Mail.SmtpClient for new code. I sent every message below on .NET 10 to a local test SMTP server, so you’ll … Read more >>

How to Check for Null in C# (is null, ?., ??, Empty Objects)

Check for null in C#: an overloaded == operator makes cart == null print True while cart is null prints False

The usual way to check for null in C#, if (order == null), once skipped real orders in a client’s checkout. Their Order class overloaded ==, so an empty cart compared equal to null. Quick answer: write order is null or order is not null. The compiler never calls an overloaded operator for those patterns. … Read more >>

C# Field Naming Conventions: _camelCase, s_ and PascalCase (.NET 10)

C# field naming convention: name = name; flagged by CS1717 becomes _name = name; with an _camelCase private field

Most field bugs we catch in code review start with name = name; inside a constructor. It compiles with a warning, and the field stays empty, because the parameter hides the field. Quick answer: the C# field naming conventions are _camelCase for private and internal fields, s_ and t_ prefixes for static and thread-static fields, … Read more >>

How to Convert Int to Hex String with Leading Zeros in C#

Convert int to hex string with leading zeros in C#: 255 formatted with ToString("X4") as 00FF

To convert int to hex string with leading zeros in C#, call ToString(“X4”) and put the digit count you want after the X. Use a lowercase x for lowercase digits: That’s 255 printed as 00FF, 000000ff and 0xFF. I ran every example below on .NET 10, including the negative numbers and byte arrays that trip … Read more >>

How to Create a GUID in C# (.NET 10 Examples + Video)

Create a GUID in C#: Guid.NewGuid() returns a version 4 GUID and Guid.CreateVersion7() a time-ordered version 7 GUID, 36 characters long

To create a GUID in C#, call Guid.NewGuid(). It gives you a random 128-bit value in the familiar 8-4-4-4-12 hex layout, so your code can make an order ID or database key without asking a server for the next number. On .NET 9 and later you also get Guid.CreateVersion7(), which puts a timestamp up front … Read more >>