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

Interface in C# with Real-Time Example

Interface in C#: IPaymentGateway with a default Quote method, implemented by BankLink, prints Fee $0.68 and the capped Fee $5.00

Write your first interface in C#, skip one member in the class that implements it, and the build stops cold: An interface is a contract. Every class that lists it must implement every member, so adding the missing Fee method fixes CS0535. We’ll build a three-gateway checkout on .NET 10, one interface skill per step. … Read more >>