What’s New in C# 14: Every Feature With Runnable Examples

What's new in C# 14: an extension block adding a Total extension property to decimal[] with the real output 2150

What’s new in C# 14? Extension members (extension properties and static extension members), the field keyword, null-conditional assignment with ?.=, nameof(List<>), implicit span conversions and a few smaller upgrades. In this tutorial, I’ll run what’s new in C# 14 one feature at a time. Each one gets a tiny .NET 10 program, its real output … 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 >>

Events in C#: Build an Order Service Step by Step (.NET 10)

Events in C#: an OrderPlaced event raised by OrderService runs the audit and email subscribers attached with +=

Our Seattle warehouse team wanted an audit line for every order and an email when a standing desk drops below five units. Events in C# handle that cleanly: the order code announces what happened and never needs to know who’s listening. In this tutorial, I’ll build that order service step by step and show you … Read more >>

How to Convert int to double with 2 Decimal Places in C#

C# 2 decimal places: Math.Round vs F2 code with real output

Prices that print as 25 instead of 25.00 look broken to anyone reading a receipt. The fix for C# 2 decimal places is short: convert the int to a double and call ToString(“F2”), or round the value with Math.Round(value, 2). In this tutorial, I’ll show you every way I handle C# 2 decimal places in … 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 Add Leading Zeros to a String or Number in C#

C# leading zeros: PadLeft turns ZIP code 2108 into 02108 and D5 formats invoice 42 as INV-00042

Turning 42 into 00042 takes one call in C#: “42”.PadLeft(5, ‘0’) for a string, or 42.ToString(“D5”) for an int. Both give you a five-character result with zeros in front. In this tutorial, I’ll show every way I use to add C# leading zeros: PadLeft, the D and 00000 formats, interpolation, negative numbers, values longer than … Read more >>

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

Convert string to int with leading zeros in C#: int.Parse turns "00042" into 42 and ToString("D5") formats it back as 00042

Parsing “00042” in C# gives you 42, every time. When you convert a string to int, leading zeros are dropped, because an int stores a value, not the digits you typed. In this tutorial, I’ll pin down how to convert string to int with leading zeros in C# on .NET 10: parse the number, then … Read more >>