---
title: "Provide default configuration to your .NET applications"
excerpt: "Learn how to provide default options into your .NET configuration to reduce redundancy"
publishDate: "2022-05-23T00:00:00"
createDate: "2022-06-24T04:50:37.143"
updateDate: "2022-08-29T05:44:05.647"
category: "dotnet"
categoryName: ".NET"
tags: [".NET","C#","Configuration"]
permalink: "/blog/dotnet/provide-default-configuration-to-your-dotnet-applications"
canonical: "https://www.twilio.com/en-us/blog/developers/best-practices/provide-default-configuration-to-dotnet-applications"
image: {"src":"/media/at5f0iei/provide-default-configuration-to-your-net-applications.png"}
imageAlt: "Provide default configuration to your .NET applications"
imageBackgroundColor: ""
thumbnailImage: {"src":"/media/kcceyrkd/dotnet-bot.png"}
thumbnailImageAlt: ""
metaTitle: "Provide default configuration to your .NET applications"
metaDescription: "Learn how to provide default options into your .NET configuration to reduce redundancy"
ogTitle: "Provide default configuration to your .NET applications"
ogDescription: "Learn how to provide default options into your .NET configuration to reduce redundancy"
ogImage: {"src":"/media/cbkp4r3x/provide-default-configuration-to-your-net-applications.png"}
ogImageAlt: "Provide default configuration to your .NET applications"
relatedPostKeys: ["4fc85f88cae74075a97c5957471af56e","658fea26998c4a39968ebe341b1518f6","e8911bbfbdfe4c05bd5aaf8040221a7e"]
noIndex: false
noFollow: false
sitemapExclude: true
source: {"nodeId":1602,"contentKey":"455bd6e6a221427eb6aa32ea8778f70e","sortOrder":139}
---


**This blog post was written for <a href="https://www.twilio.com/" target="_top">Twilio</a> and <a href="https://www.twilio.com/blog/provide-default-configuration-to-dotnet-applications" target="_top">originally published at&nbsp;the Twilio blog</a>.**




<p>In a previous post I shared <a href="https://www.twilio.com/blog/better-configuration-csharp-dotnet-for-twilio">how you can better configure your .NET applications for Twilio</a> and <a href="https://www.twilio.com/blog/better-configure-csharp-and-dotnet-apps-for-sendgrid">SendGrid</a> using the Microsoft.Extensions.Configuration APIs. In this tutorial, I'll build upon the techniques from those tutorials and you'll learn how to store default options in one section of your configuration and override them with specific options from another section. Let me clarify using an example. Applications often send different types of emails, like welcome emails, password reset emails, offer emails, etc. Depending on the type of email, you may want to send them from a different email address. For most emails, you may be using something like 'no-reply@yourdomain.tld', but for offer emails, the replies to the email could be of value. So instead of using the same no-reply address, you could use an email address that will be routed to a sales person and maybe automatically integrates with a Customer Relationship Management (CRM) system.</p>



<p>In this example, you'd want to store 'no-reply@yourdomain.tld' as the default reply address, but for offer-emails, you want to use another email address, for example, 'offers@yourdomain.tld'.</p>



## Prerequisites {#prerequisites}

To follow along, I recommend having

- Some experience with C# and .NET
- An OS that supports .NET (Windows/macOS/Linux)
- <a href="https://git-scm.com/downloads">Git CLI</a>
- <a href="https://dotnet.microsoft.com/en-us/download/dotnet/6.0">.NET 6 SDK</a> (older or newer should work too)
- A code editor or IDE (I recommend <a href="https://code.visualstudio.com/Download">VS Code</a> with <a href="https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp">the C# plugin</a>, <a href="https://visualstudio.microsoft.com/">Visual Studio</a>, or <a href="https://www.jetbrains.com/rider/">JetBrains Rider</a>)




You can find [the source code for this tutorial on GitHub](https://github.com/Swimburger/DotNetDefaultConfiguration). Use it as a reference if you run into any issues, or [submit an issue](https://github.com/Swimburger/DotNetDefaultConfiguration/issues) if you need assistance.




## Getting started {#getting-started}




You can apply these concepts to any scenario, but let's continue using the email scenario throughout this tutorial. 


Run the following commands to clone the tutorial project to your machine using git and navigate into the project:

<pre style="color:#000000;background:#ffffff;">git clone https:<span style="color:#3f3fbf; ">/</span><span style="color:#3f3fbf; ">/github.com/Swimburger/DotNetDefaultConfiguration.git</span>  --branch start&#10;<span style="color:#7f0055; font-weight:bold; ">cd</span> DotNetDefaultConfiguration&#10;</pre>

The git repository you cloned contains a .NET 6 console application that already contains some JSON configuration and code to load the configuration.

Here's what the *appsettings.json* file looks like:

<pre style="color:#000000;background:#ffffff;">{&#10;  <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails</span><span style="color:#2a00ff; ">"</span>: {&#10;    <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Welcome</span><span style="color:#2a00ff; ">"</span>: {&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">no-reply@yourdomain.tld</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromName</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Your App Name</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToEmail</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">jon@doe.com</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToName</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Jon Doe</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Subject</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Welcome to our service!</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Body</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Thank you for signing up!</span><span style="color:#2a00ff; ">"</span>&#10;    },&#10;    <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">PasswordReset</span><span style="color:#2a00ff; ">"</span>: {&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">no-reply@yourdomain.tld</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromName</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Your App Name</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToEmail</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">jon@doe.com</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToName</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Jon Doe</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Subject</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Reset your password</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Body</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Click the link below to reset your password.</span><span style="color:#2a00ff; ">"</span>&#10;    },&#10;    <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Offer</span><span style="color:#2a00ff; ">"</span>: {&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">offers@yourdomain.tld</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromName</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Jane</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToEmail</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">jon@doe.com</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToName</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Jon Doe</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Subject</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Don't miss this offer!</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Body</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">We have an amazing offer for you!</span><span style="color:#2a00ff; ">"</span>&#10;    }&#10;  }&#10;}&#10;</pre>

Here's what the *Program.cs* file looks like:

<pre style="color:#000000;background:#ffffff;"><span style="color:#7f0055; font-weight:bold; ">using</span> Microsoft.Extensions.Configuration;&#10;&#10;IConfiguration config = <span style="color:#7f0055; font-weight:bold; ">new</span> ConfigurationBuilder()&#10;    .AddJsonFile(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">appsettings.json</span><span style="color:#2a00ff; ">"</span>)&#10;    .Build();&#10;    &#10;EmailOptions GetEmailOptions(<span style="color:#7f0055; font-weight:bold; ">string</span> emailSectionName)&#10;{&#10;    <span style="color:#7f0055; font-weight:bold; ">var</span> emailOptions = <span style="color:#7f0055; font-weight:bold; ">new</span> EmailOptions();&#10;    &#10;    <span style="color:#7f0055; font-weight:bold; ">var</span> emailsSection = config.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails</span><span style="color:#2a00ff; ">"</span>);&#10;    <span style="color:#7f0055; font-weight:bold; ">var</span> emailSection = emailsSection.GetSection(emailSectionName);&#10;&#10;    emailOptions.FromEmail = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.FromName = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromName</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.ToEmail = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToEmail</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.ToName = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToName</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.Subject = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Subject</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.Body = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Body</span><span style="color:#2a00ff; ">"</span>];&#10;&#10;    <span style="color:#7f0055; font-weight:bold; ">return</span> emailOptions;&#10;}&#10;&#10;<span style="color:#7f0055; font-weight:bold; ">void</span> LogEmailOptions(EmailOptions emailOptions)&#10;{&#10;    Console.WriteLine(<span style="color:#2a00ff; ">@"</span><span style="color:#2a00ff; ">Logging Email Option:</span>&#10;<span style="color:#2a00ff; ">&nbsp;&nbsp;&nbsp;&nbsp;FromEmail: {0}</span>&#10;<span style="color:#2a00ff; ">&nbsp;&nbsp;&nbsp;&nbsp;FromName: {1}</span>&#10;<span style="color:#2a00ff; ">&nbsp;&nbsp;&nbsp;&nbsp;ToEmail: {2}</span>&#10;<span style="color:#2a00ff; ">&nbsp;&nbsp;&nbsp;&nbsp;ToName: {3}</span>&#10;<span style="color:#2a00ff; ">&nbsp;&nbsp;&nbsp;&nbsp;Subject: {4}</span>&#10;<span style="color:#2a00ff; ">&nbsp;&nbsp;&nbsp;&nbsp;Body: {5}</span><span style="color:#2a00ff; ">"</span>, &#10;        emailOptions.FromEmail, &#10;        emailOptions.FromName, &#10;        emailOptions.ToEmail, &#10;        emailOptions.ToName, &#10;        emailOptions.Subject, &#10;        emailOptions.Body&#10;    );&#10;}&#10;&#10;<span style="color:#7f0055; font-weight:bold; ">var</span> welcomeEmail = GetEmailOptions(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Welcome</span><span style="color:#2a00ff; ">"</span>);&#10;LogEmailOptions(welcomeEmail);&#10;&#10;<span style="color:#7f0055; font-weight:bold; ">var</span> passwordResetEmail = GetEmailOptions(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">PasswordReset</span><span style="color:#2a00ff; ">"</span>);&#10;LogEmailOptions(passwordResetEmail);&#10;&#10;<span style="color:#7f0055; font-weight:bold; ">var</span> offerEmail = GetEmailOptions(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Offer</span><span style="color:#2a00ff; ">"</span>);&#10;LogEmailOptions(offerEmail);&#10;&#10;<span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">class</span> EmailOptions&#10;{&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> FromEmail { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> FromName { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> ToEmail { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> ToName { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> Subject { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> Body { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;}&#10;</pre>

The code builds configuration using only JSON as a source and then the GetEmailOptions local function is used to fetch the configuration and create an instance of  EmailOptions for the welcome-email, password-reset-email, and offer-email. You would probably dynamically generate the body of the emails and dynamically fetch the recipient email address in a real application, but this will work fine for the purpose of this tutorial.

After each EmailOptions object is created for each email section, the configuration is logged via the LogEmailOptions local function, so you can see the result of this code.

You may notice that there's a lot of redundancy in the JSON, which is why you'll introduce a defaults section in the next part of this tutorial.

Run the project using the .NET CLI:

<pre style="color:#000000;background:#ffffff;">dotnet run&#10;<span style="color:#3f7f59; "># Output:</span>&#10;<span style="color:#3f7f59; ">#  Logging Email Option:</span>&#10;<span style="color:#3f7f59; ">#      FromEmail: </span><span style="color:#3f3fbf; ">no-reply@yourdomain.tld</span>&#10;<span style="color:#3f7f59; ">#      FromName: Your App Name</span>&#10;<span style="color:#3f7f59; ">#      ToEmail: </span><span style="color:#3f3fbf; ">jon@doe.com</span>&#10;<span style="color:#3f7f59; ">#      ToName: Jon Doe</span>&#10;<span style="color:#3f7f59; ">#      Subject: Welcome to our service!</span>&#10;<span style="color:#3f7f59; ">#      Body: Thank you for signing up!</span>&#10;<span style="color:#3f7f59; ">#  Logging Email Option:</span>&#10;<span style="color:#3f7f59; ">#      FromEmail: </span><span style="color:#3f3fbf; ">no-reply@yourdomain.tld</span>&#10;<span style="color:#3f7f59; ">#      FromName: Your App Name</span>&#10;<span style="color:#3f7f59; ">#      ToEmail: </span><span style="color:#3f3fbf; ">jon@doe.com</span>&#10;<span style="color:#3f7f59; ">#      ToName: Jon Doe</span>&#10;<span style="color:#3f7f59; ">#      Subject: Reset your password</span>&#10;<span style="color:#3f7f59; ">#      Body: Click the link below to reset your password.</span>&#10;<span style="color:#3f7f59; ">#  Logging Email Option:</span>&#10;<span style="color:#3f7f59; ">#      FromEmail: </span><span style="color:#3f3fbf; ">offers@yourdomain.tld</span>&#10;<span style="color:#3f7f59; ">#      FromName: Jane</span>&#10;<span style="color:#3f7f59; ">#      ToEmail: </span><span style="color:#3f3fbf; ">jon@doe.com</span>&#10;<span style="color:#3f7f59; ">#      ToName: Jon Doe</span>&#10;<span style="color:#3f7f59; ">#      Subject: Don't miss this offer!</span>&#10;<span style="color:#3f7f59; ">#      Body: We have an amazing offer for you!</span>&#10;</pre>

You can see that the configuration is pulled from the JSON file and then written to the console.

## Add a defaults section for redundant configuration elements {#add-a-defaults-section-for-redundant-configuration-elements}

You can add a Defaults section to remove the redundant configuration elements. Open *appsettings.json* and in front of the Welcome section, add a Defaults section:

<pre style="color:#000000;background:#ffffff;">{&#10;  <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails</span><span style="color:#2a00ff; ">"</span>: {&#10;    <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Defaults</span><span style="color:#2a00ff; ">"</span>: {&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">no-reply@yourdomain.tld</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromName</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Your App Name</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToEmail</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">jon@doe.com</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToName</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Jon Doe</span><span style="color:#2a00ff; ">"</span>&#10;    },&#10;    <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Welcome</span><span style="color:#2a00ff; ">"</span>: {&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Subject</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Welcome to our service!</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Body</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Thank you for signing up!</span><span style="color:#2a00ff; ">"</span>&#10;    },&#10;    <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">PasswordReset</span><span style="color:#2a00ff; ">"</span>: {&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Subject</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Reset your password</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Body</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Click the link below to reset your password.</span><span style="color:#2a00ff; ">"</span>&#10;    },&#10;    <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Offer</span><span style="color:#2a00ff; ">"</span>: {&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">offers@yourdomain.tld</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromName</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Jane</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Subject</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Don't miss this offer!</span><span style="color:#2a00ff; ">"</span>,&#10;      <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Body</span><span style="color:#2a00ff; ">"</span>: <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">We have an amazing offer for you!</span><span style="color:#2a00ff; ">"</span>&#10;    }&#10;  }&#10;}&#10;</pre>

As shown above, also remove the redundant FromEmail, FromName, ToEmail, and ToName properties from the Welcome and PasswordReset section. However, only remove the redundant ToEmail and ToName properties from the  Offer section and leave the FormEmail and FromName properties be.

Now, update the code in the *Program.cs* file so that the configuration is retrieved from the specific email section, but if null (??), then retrieve it from the Defaults section.

<pre style="color:#000000;background:#ffffff;">EmailOptions GetEmailOptions(<span style="color:#7f0055; font-weight:bold; ">string</span> emailSectionName)&#10;{&#10;    <span style="color:#7f0055; font-weight:bold; ">var</span> emailOptions = <span style="color:#7f0055; font-weight:bold; ">new</span> EmailOptions();&#10;    &#10;    <span style="color:#7f0055; font-weight:bold; ">var</span> emailsSection = config.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails</span><span style="color:#2a00ff; ">"</span>);&#10;    <span style="color:#7f0055; font-weight:bold; ">var</span> defaultsSections = emailsSection.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Defaults</span><span style="color:#2a00ff; ">"</span>);&#10;    <span style="color:#7f0055; font-weight:bold; ">var</span> emailSection = emailsSection.GetSection(emailSectionName);&#10;&#10;    emailOptions.FromEmail = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>] ?? defaultsSections[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.FromName = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromName</span><span style="color:#2a00ff; ">"</span>] ?? defaultsSections[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromName</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.ToEmail = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToEmail</span><span style="color:#2a00ff; ">"</span>] ?? defaultsSections[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToEmail</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.ToName = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToName</span><span style="color:#2a00ff; ">"</span>] ?? defaultsSections[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">ToName</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.Subject = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Subject</span><span style="color:#2a00ff; ">"</span>] ?? defaultsSections[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Subject</span><span style="color:#2a00ff; ">"</span>];&#10;    emailOptions.Body = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Body</span><span style="color:#2a00ff; ">"</span>] ?? defaultsSections[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Body</span><span style="color:#2a00ff; ">"</span>];&#10;&#10;    <span style="color:#7f0055; font-weight:bold; ">return</span> emailOptions;&#10;}&#10;</pre>



<p class="panel panel--info">?? is the <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator">null-coalescing operator</a> which returns the value on the right side if the value on the left side is null. This allows you to succinctly check for null and if null, provide a fallback value.</p>

Run the project again using the .NET CLI:

<pre style="color:#000000;background:#ffffff;">dotnet run&#10;</pre>




As you can see, the output has not changed, but now you can set configuration on specific sections and fallback on configuration from the Defaults section. 


This code gets the job done, but the same result could be achieved more eloquently by binding configuration sections to strongly-typed objects.

## Bind defaults and override defaults using the configuration binder {#bind-defaults-and-override-defaults-using-the-configuration-binder}




Instead of retrieving each configuration element individually, you can use the configuration binder to bind configuration sections to strongly-typed objects. There are two methods you can use to bind configuration sections to an instance of EmailOptions: Get&lt;T&gt;() and  Bind(object).


To use the Get method, pass in the desired type as a generic type parameter and the Get method create an instance of that type and bind the configuration to the object.

<pre style="color:#000000;background:#ffffff;"><span style="color:#7f0055; font-weight:bold; ">var</span> emailsSection = config.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails</span><span style="color:#2a00ff; ">"</span>);&#10;<span style="color:#7f0055; font-weight:bold; ">var</span> emailOptions = emailsSection.GetSection(emailSectionName)&#10;    .Get&lt;EmailOptions&gt;();&#10;</pre>

To use the Bind method, create an instance of EmailOptions and then pass it as a parameter to the Bind method.

<pre style="color:#000000;background:#ffffff;"><span style="color:#7f0055; font-weight:bold; ">var</span> emailOptions = <span style="color:#7f0055; font-weight:bold; ">new</span> EmailOptions();&#10;&#10;<span style="color:#7f0055; font-weight:bold; ">var</span> emailsSection = config.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails</span><span style="color:#2a00ff; ">"</span>);&#10;emailsSection.GetSection(emailSectionName)&#10;    .Bind(emailOptions);&#10;</pre>

What's even more interesting is that you can use the Bind method multiple times! So, if you'd like to bind both the defaults and the email specific configuration, you could do that as follows:

<pre style="color:#000000;background:#ffffff;"><span style="color:#7f0055; font-weight:bold; ">var</span> emailOptions = <span style="color:#7f0055; font-weight:bold; ">new</span> EmailOptions();&#10;&#10;<span style="color:#7f0055; font-weight:bold; ">var</span> emailsSection = config.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails</span><span style="color:#2a00ff; ">"</span>);&#10;emailsSection.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Defaults</span><span style="color:#2a00ff; ">"</span>).Bind(emailOptions);&#10;emailsSection.GetSection(emailSectionName).Bind(emailOptions);&#10;</pre>

Using this technique, update the GetEmailOptions local function to create an instance of  EmailOptions, then bind the Defaults section to it, and then override the defaults by binding the specific section to it:

<pre style="color:#000000;background:#ffffff;">EmailOptions GetEmailOptions(<span style="color:#7f0055; font-weight:bold; ">string</span> emailSectionName)&#10;{&#10;    <span style="color:#7f0055; font-weight:bold; ">var</span> emailOptions = <span style="color:#7f0055; font-weight:bold; ">new</span> EmailOptions();&#10;    &#10;    <span style="color:#7f0055; font-weight:bold; ">var</span> emailsSection = config.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails</span><span style="color:#2a00ff; ">"</span>);&#10;    emailsSection.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Defaults</span><span style="color:#2a00ff; ">"</span>).Bind(emailOptions);&#10;    emailsSection.GetSection(emailSectionName).Bind(emailOptions);&#10;&#10;    <span style="color:#7f0055; font-weight:bold; ">return</span> emailOptions;&#10;}&#10;</pre>



## Hardcoding defaults {#hardcoding-defaults}

In addition to pulling defaults from the configuration, you may also opt to add default values directly into your code. For example, if you're developing a library for others to consume, it would be convenient for some of the configuration elements to have default values.

If you're pulling individual configuration elements, you could add default values like this:

<pre style="color:#000000;background:#ffffff;">emailOptions.FromEmail = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>]&#10;    ?? defaultsSections[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>]&#10;    ?? <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">no-reply@yourdomain.tld</span><span style="color:#2a00ff; ">"</span>;&#10;</pre>

Maybe it doesn't make sense to hardcode a default value, but the configuration element is required, so you want to throw an exception if the configuration element is null. In that case, you can also use the null-coalescing operator to throw an exception inline like this:

<pre style="color:#000000;background:#ffffff;">emailOptions.FromEmail = emailSection[<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">FromEmail</span><span style="color:#2a00ff; ">"</span>]&#10;    ?? <span style="color:#7f0055; font-weight:bold; ">throw</span> <span style="color:#7f0055; font-weight:bold; ">new</span> Exception($<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails:{emailSectionName}:FromEmail has to be configured</span><span style="color:#2a00ff; ">"</span>);&#10;</pre>

However, if you're using the configuration binder, you don't need to use the null-coalescing operator, and you can instead specify the default values in the object initializer like this:

<pre style="color:#000000;background:#ffffff;"><span style="color:#7f0055; font-weight:bold; ">var</span> emailOptions = <span style="color:#7f0055; font-weight:bold; ">new</span> EmailOptions&#10;{&#10;    FromEmail = <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">no-reply@yourdomain.tld</span><span style="color:#2a00ff; ">"</span>,&#10;    ...&#10;};&#10;&#10;<span style="color:#7f0055; font-weight:bold; ">var</span> emailsSection = config.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Emails</span><span style="color:#2a00ff; ">"</span>);&#10;emailsSection.GetSection(<span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">Defaults</span><span style="color:#2a00ff; ">"</span>).Bind(emailOptions);&#10;emailsSection.GetSection(emailSectionName).Bind(emailOptions);&#10;</pre>

Alternatively, you could specify default values in the class definition itself like this:

<pre style="color:#000000;background:#ffffff;"><span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">class</span> EmailOptions&#10;{&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> FromEmail { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; } = <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">no-reply@yourdomain.tld</span><span style="color:#2a00ff; ">"</span>;&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> FromName { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> ToEmail { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> ToName { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> Subject { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;    <span style="color:#7f0055; font-weight:bold; ">public</span> <span style="color:#7f0055; font-weight:bold; ">string</span> Body { <span style="color:#7f0055; font-weight:bold; ">get</span>; <span style="color:#7f0055; font-weight:bold; ">set</span>; }&#10;}&#10;</pre>



## Next steps {#next-steps}




You've learned how to provide default configuration values through code and through a dedicated Defaults configuration section. If you haven't already, I recommend learning about more [configuration techniques in a tutorial applied to Twilio configuration](https://www.twilio.com/blog/better-configuration-csharp-dotnet-for-twilio), or applied to [SendGrid configuration](https://www.twilio.com/blog/better-configure-csharp-and-dotnet-apps-for-sendgrid).


If you found this useful, let me know and share what you're working on. I can't wait to see what you build!
