title: Implement custom section with a Configuration Element Collection c#
body: <p>I would like to implement a custom configuration section in a project. But something I don't understand so dont work.</p>
  <p>I have App.config that looks like this:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name=&quot;DepartmentConfigurationSection&quot; type=&quot;Statistics.Config.DepartmentSection , Program1&quot;/&gt;
    &lt;/configSections&gt;
    &lt;s&gt;
      &lt;Cash&gt;
        &lt;add Number=&quot;1&quot; Name=&quot;Money&quot; /&gt;
      &lt;/Cash&gt;
      &lt;Departments&gt;
        &lt;add Id=&quot;1&quot; Name=&quot;x&quot; /&gt;
        &lt;add Id=&quot;2&quot; Name=&quot;y&quot; /&gt;
      &lt;/Departments&gt;
    &lt;/s&gt;
  &lt;/configuration&gt;
  </code></pre>
  <p>I create a file called <strong>DepartmentSection.cs</strong> that cointains the ConfigurationElement,ConfigurationElementCollection and the ConfigurationSection.
  The class is like this:</p>
  <pre><code> public class DepartmentConfig : ConfigurationElement
      {
          public DepartmentConfig() { }
  
          public DepartmentConfig(int id, string name)
          {
              Id = id;
              Name = name;
          }
  
          [ConfigurationProperty(&quot;Id&quot;, IsRequired = true, IsKey = true)]
          public int Id
          {
              get { return (int)this[&quot;Id&quot;]; }
              set { this[&quot;Id&quot;] = value; }
          }
  
          [ConfigurationProperty(&quot;Name&quot;,  IsRequired = true, IsKey = false)]
          public string Name
          {
              get { return (string)this[&quot;Name&quot;]; }
              set { this[&quot;Name&quot;] = value; }
          }
      }
  
  
      public class DepartmentCollection : ConfigurationElementCollection
      {
          public DepartmentCollection()
          {
              Console.WriteLine(&quot;ServiceCollection Constructor&quot;);
          }
  
          public DepartmentConfig this[int index]
          {
              get { return (DepartmentConfig)BaseGet(index); }
              set
              {
                  if (BaseGet(index) != null)
                  {
                      BaseRemoveAt(index);
                  }
                  BaseAdd(index, value);
              }
          }
  
          public void Add(DepartmentConfig depConfig)
          {
              BaseAdd(depConfig);
          }
  
          public void Clear()
          {
              BaseClear();
          }
  
          protected override ConfigurationElement CreateNewElement()
          {
              return new DepartmentConfig();
          }
  
          protected override object GetElementKey(ConfigurationElement element)
          {
              return ((DepartmentConfig)element).Id;
          }
  
          public void Remove(DepartmentConfig depConfig)
          {
              BaseRemove(depConfig.Id);
          }
  
          public void RemoveAt(int index)
          {
              BaseRemoveAt(index);
          }
  
          public void Remove(string name)
          {
              BaseRemove(name);
          }
      }
  
  
      public class DepartmentConfigurationSection : ConfigurationSection
      {
          [ConfigurationProperty(&quot;Departments&quot;, IsDefaultCollection = false)]
          [ConfigurationCollection(typeof(DepartmentCollection),
              AddItemName = &quot;add&quot;,
              ClearItemsName = &quot;clear&quot;,
              RemoveItemName = &quot;remove&quot;)]
          public DepartmentCollection Departments
          {
              get
              {
                  return (DepartmentCollection)base[&quot;Departments&quot;];
              }
          }
      }
  </code></pre>
  <p>I tried to get the collection from the handler but without success. I tried like this but give me this error: &quot;Unable to initialize the system configuration&quot;.</p>
  <pre><code>    DepartmentConfigurationSection serviceConfigSection =
      ConfigurationManager.GetSection(&quot;s&quot;) as DepartmentConfigurationSection;
  
      DepartmentConfig serviceConfig = serviceConfigSection.Departments[0];
  </code></pre>