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 >>

Hashtable in C#: Add, ContainsKey, Remove, Loop and Dictionary (.NET 10)

Hashtable in C#: Add stores badge 1017, a missing key returns null, and a duplicate Add throws ArgumentException

To use a Hashtable in C#, add using System.Collections;, create new Hashtable(), store pairs with Add(key, value) and cast each value back to its real type when you read it. For new code, Microsoft recommends the generic Dictionary<TKey,TValue> instead. I ran every example here on .NET 10 against one staff table, including the exceptions that … Read more >>

ArrayList in C#: Add, Sort, Search, Casting and List<T> (.NET 10)

ArrayList in C#: an ArrayList holding a string, an int and a decimal, where (double)row[2] throws InvalidCastException

An ArrayList in C# is a resizable list from System.Collections that stores everything as object. Create it with new ArrayList(), call Add, and cast each item back when you read it: Output: I ran every example below on .NET 10, including the casting errors, and finished by converting the list to the List<T> Microsoft recommends … Read more >>

Enum in C#: Declare, Convert, Flags and JSON (.NET 10)

Enum in C#: a table of Enum.TryParse with ignoreCase and Enum.IsDefined results for Shipped, shipped, Lost, 42 and 2

Status code 3 means nothing to whoever reads your code next year. Shipped means everything, and the compiler can check it for you. Quick answer: An enum in C# is a value type that gives names to a fixed set of integer constants. Declare it with enum OrderStatus { Pending, Paid, Shipped }, cast it … Read more >>

C# Array: How to Declare, Use and Add Values to Arrays in C#

C# array of five US states with indexes, states[1] and states[^1]

Last spring, a sales dashboard I was building for a US retail client crashed on one innocent line: regions[4]. The array held four regions, and index 4 simply doesn’t exist. Here’s the quick answer. A C# array is a fixed-size, zero-indexed block of values of one type, such as string[] regions = [“West”, “South”];. You … Read more >>