WPF How to use Resource Dictionary in C#

To know what is Resource Dictionary,example of Resource dictionary, how we can add Resource Dictionary and how we can use it from XAML visit here:

Now there may be a case where we need to access a resource dictionary that we have defined in our project, from C# code.
If we have already merged our resource dictionary in XAML, it is easy to access the inner resources using thecontrol.FindResource("<<ResourceKey>>"); method.
 But, if we haven’t merged the resources in XAML and we still need to use the resource dictionary, we have options to use it directly inC# code. 
We have modified the example given here WPF How to use Resource Dictionary in XAML. where we were using ResourceDictionary from XAML.
Below is the XAML and code snippet:
XAML Code:
<UserControl x:Class="WPF_Sample_Project_Test.ResourceDictionaryExample"
             d:DesignHeight="300" d:DesignWidth="300">
        <Button x:Name="btnRound"  />
C# Code:
public partial class ResourceDictionaryExample : UserControl
        private ResourceDictionary _myResourceDictionary;
        public ResourceDictionaryExample()
        public void LoadResourceDictionary()
            _myResourceDictionary = new ResourceDictionary();
            _myResourceDictionary.Source =
                new Uri("/WPF Sample Project Test;component/Resources/MyResourceDictionary.xaml",
        public void ApplyStyle()
            Style mybuttonstyle = _myResourceDictionary["RoundButtonStyle"] as Style;
            btnRound.Style = mybuttonstyle;

We have used a URI to hold the Resource Dictionary content. When we define the URI, the project name comes first and then the relative path. The UriKind option is very important. If we don’t mention the UriKind, it will not be able to find the resource.
As it is a resource dictionary, we can access styles by their keys. In the above example we have used key “RoundButtonStyle” to access the Button Style.

Note: The output will be same whether we use Resource Dictionary From XAML or from C#.