TryGetValue and TryParse patterns.
The Problem
Standard try-patterns require verbose out parameters:The Solution
Dictionary Extensions
Reference Types
Value Types
With Default Values
Parsing Extensions
All parsing methods returnnull on failure instead of using out parameters.
Integer Parsing
Floating-Point Parsing
Boolean Parsing
Character Parsing
GUID Parsing
Enum Parsing
Date/Time Parsing
Collection Access
Safe index-based access withoutIndexOutOfRangeException:
Reference Types
Value Types
With Default Value
Examples
Configuration Parsing
Query String Parsing
Safe Array Access
API Reference
Dictionary Methods
| Method | Description |
|---|---|
GetOrNull<TKey, TValue>(key) | Returns value or null (reference types) |
GetValueOrNull<TKey, TValue>(key) | Returns value or null (value types) |
GetOrDefault<TKey, TValue>(key, default) | Returns value or specified default |
GetOrElse<TKey, TValue>(key, factory) | Returns value or factory result |
Parsing Methods
| Method | Input | Output |
|---|---|---|
TryParseByte | string | byte? |
TryParseSByte | string | sbyte? |
TryParseInt16 | string | short? |
TryParseUInt16 | string | ushort? |
TryParseInt32 | string | int? |
TryParseUInt32 | string | uint? |
TryParseInt64 | string | long? |
TryParseUInt64 | string | ulong? |
TryParseSingle | string | float? |
TryParseDouble | string | double? |
TryParseDecimal | string | decimal? |
TryParseBool | string | bool? |
TryParseChar | string | char? |
TryParseGuid | string | Guid? |
TryParseEnum<T> | string | T? |
TryParseDateTime | string | DateTime? |
TryParseDateTimeOffset | string | DateTimeOffset? |
TryParseTimeSpan | string | TimeSpan? |
Collection Methods
| Method | Description |
|---|---|
ElementAtOrNull<T>(index) | Returns element or null (reference types) |
ValueAtOrNull<T>(index) | Returns element or null (value types) |
ElementAtOrDefault<T>(index, default) | Returns element or specified default |
