Extensions.Get |
Gets the value or default.
Namespace: Gamanet.C4.SimpleInterfaces
Assembly: Gamanet.C4.SimpleInterfaces (in Gamanet.C4.SimpleInterfaces.dll) Version: 2026.0.0.242
Syntax
C#
public static TValue GetValueOrDefault<TValue>( this IDictionary<string, Object> dictionary, string key )
Parameters
- dictionary IDictionary<String, Object>
- The dictionary.
- key String
- The key.
Type Parameters
- TValue
- The type of the value.
Return Value
TValuevalue
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type IDictionary<String, Object>. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).Example
The following example illustrates how to get value or default from dictionary
C#
using System; using System.Collections.Generic; using Gamanet.C4.SimpleInterfaces; public static class IsNullOrEmptyString { static void Main() { var dictionary = new Dictionary<string, object> { { "one", 1 }, { "two", 22 }, { "three", 333 }, { "four", 4444 }, { "five", 55555 } }; // find value by not existing key string to get default value var result = dictionary.GetValueOrDefault<int>("nine"); Console.WriteLine(result); // find value by key string result = dictionary.GetValueOrDefault<int>("three"); Console.WriteLine(result); // find value by key string result = dictionary.GetValueOrDefault<int>("five"); Console.WriteLine(result); // The example displays the following output: 0 333 55555 } }
See Also