dotnet-api-docs icon indicating copy to clipboard operation
dotnet-api-docs copied to clipboard

Update summary for ConfigurationBinder.GetValue<T>(...) to clarify default value return

Open MashalMohammed opened this issue 1 year ago • 1 comments

Summary

This PR clarifies return of default value in ConfigurationBinder.GetValue<T>(...).

ConfigurationBinder.GetValue<T>(...) returns null if the specified key is not found in the IConfiguration, which is not apparent or documented, and user might expect an error, incorrectly. Sibling variant methods has a default value param at the least.

namespace Microsoft.Extensions.Configuration
{
    /// <summary>
    /// Static helper class that allows binding strongly typed objects to configuration values.
    /// </summary>
    public static class [ConfigurationBinder]
    {
    
        ...
    
        /// <summary>
        /// Extracts the value with the specified key and converts it to type T.
        /// </summary>
        /// <typeparam name="T">The type to convert the value to.</typeparam>
        /// <param name="configuration">The configuration.</param>
        /// <param name="key">The key of the configuration section's value to convert.</param>
        /// <param name="defaultValue">The default value to use if no value is found.</param>
        /// <returns>The converted value.</returns>
        public static T? GetValue<T>(this IConfiguration configuration, string key, T defaultValue)
        {
            return (T?)GetValue(configuration, typeof(T), key, defaultValue);
        }

https://source.dot.net/#Microsoft.Extensions.Configuration.Binder/ConfigurationBinder.cs,232ab46b757d72f9

I've just mentioned that the method could return a default if the key is not present.

Fixes #9638.

MashalMohammed avatar Feb 25 '24 07:02 MashalMohammed

Learn Build status updates of commit 44dbde8:

:white_check_mark: Validation status: passed

File Status Preview URL Details
xml/Microsoft.Extensions.Configuration/ConfigurationBinder.xml :white_check_mark:Succeeded View

For more details, please refer to the build report.

For any questions, please:

Learn Build status updates of commit 4fb1c15:

:white_check_mark: Validation status: passed

File Status Preview URL Details
xml/Microsoft.Extensions.Configuration/ConfigurationBinder.xml :white_check_mark:Succeeded View

For more details, please refer to the build report.

For any questions, please: