DictionaryExtensionsGetValueOrDefaultTActualValue(IDictionaryString, Object, String, TActualValue) Method

Tries to get the typed value from a string-object dictionary for the given key.

Definition

Namespace: KGySoft.CoreLibraries
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.1.0
C#
public static TActualValue GetValueOrDefault<TActualValue>(
	this IDictionary<string, Object> dictionary,
	string key,
	TActualValue defaultValue = null
)

Parameters

dictionary  IDictionaryString, Object
The dictionary.
key  String
The key whose value to get.
defaultValue  TActualValue  (Optional)
The default value to return if key was not found or its actual type is not compatible with TActualValue This parameter is optional.
Default value: if TActualValue is a reference type; otherwise, the bitwise zero value of TActualValue.

Type Parameters

TActualValue
The type of the value with the corresponding key to get.

Return Value

TActualValue
The found value or defaultValue if key was not found or its value cannot be cast to TActualValue.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IDictionaryString, 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 demonstrates how to use the GetValueOrDefault overloads.
C#
using System;
using System.Collections.Generic;
using KGySoft.CoreLibraries;

public class Example
{
    public static void Main()
    {
        var dict = new Dictionary<string, object>
        {
            { "Int", 42 },
            { "String", "Blah" },
        };

        // old way:
        object obj;
        int intValue;
        if (dict.TryGetValue("Int", out obj) && obj is int)
            intValue = (int)obj;

        // C# 7.0 way:
        if (dict.TryGetValue("Int", out object o) && o is int i)
            intValue = i;

        // GetValueOrDefault ways:

        // TValue return type (which is object now)
        intValue = (int)dict.GetValueOrDefault("Int");

        // by defining a default value the actual type (and return type) can be specified
        intValue = dict.GetValueOrDefault("Int", 0);

        // an alternative syntax for string-object dictionaries (a default value still can be specified)
        intValue = dict.GetValueOrDefault<int>("Int");

        // to use different actual (and return) type for non string-object dictionaries use the GetActualValueOrDefault method:
        intValue = dict.GetActualValueOrDefault("Int", 0);

        // using nullable int actual type to get null if "Unknown" does not exist or is not an int
        int? intOrNull = dict.GetValueOrDefault<int?>("Unknown");

        // If obtaining a default value is an expensive operation you can use the delegate overloads.
        // The delegate is invoked only when the key was not found in the dictionary:
        intValue = (int)dict.GetValueOrDefault("Unknown", () =>
        {
            Console.WriteLine("Default value factory was invoked from GetValueOrDefault");
            return -1;
        });

          // Which is the same as:
       intValue = dict.GetActualValueOrDefault("Unknown", () =>
       {
           Console.WriteLine("Default value factory was invoked from GetActualValueOrDefault");
           return -1;
       });

       Console.WriteLine($"{nameof(intValue)}: {intValue}; {nameof(intOrNull)}: {intOrNull}");
    }
}

See Also