Type Casting in C#: A Program to Demonstrate Every Conversion

Type casting in C#: a table comparing (int) cast, Convert.ToInt32 and int.Parse results for 12.97, 2.5, 3.5, "42", "abc" and null

Type casting in C# changes a value from one type to another. Widening casts like int to long happen implicitly, while narrowing casts like (int)12.97 need explicit syntax and can lose data: that one returns 12, not 13. In this tutorial, I’ll build up a little US shipping calculator and use it to show type … Read more >>

How to Get the First and Last Day of the Month in C#

C# first and last day of the month table for October 2026, December 2026 and leap year February 2028

A month-end sales report I built for a retail client in Columbus, Ohio kept dropping orders placed on October 31. The C# code that picked the month’s first and last day looked fine, but the boundary was wrong. Here’s the short answer for C#: the first day of the month is new DateTime(d.Year, d.Month, 1), … 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 >>