<PackageReference Include="System.Configuration.ConfigurationManager" Version="9.0.0" />

ConfigurationSectionCollection

Represents a collection of related sections within a configuration file.
using System.Collections; using System.Collections.Specialized; namespace System.Configuration { public sealed class ConfigurationSectionCollection : NameObjectCollectionBase { private readonly ConfigurationSectionGroup _configSectionGroup; private MgmtConfigurationRecord _configRecord; public ConfigurationSection this[string name] { get { return Get(name); } } public ConfigurationSection this[int index] { get { return Get(index); } } internal ConfigurationSectionCollection(MgmtConfigurationRecord configRecord, ConfigurationSectionGroup configSectionGroup) : base(StringComparer.Ordinal) { _configRecord = configRecord; _configSectionGroup = configSectionGroup; IDictionaryEnumerator enumerator = _configRecord.SectionFactories.GetEnumerator(); try { while (enumerator.MoveNext()) { FactoryId factoryId = (FactoryId)((DictionaryEntry)enumerator.Current).Value; if (factoryId.Group == _configSectionGroup.SectionGroupName) BaseAdd(factoryId.Name, factoryId.Name); } } finally { (enumerator as IDisposable)?.Dispose(); } } internal void DetachFromConfigurationRecord() { _configRecord = null; BaseClear(); } private void VerifyIsAttachedToConfigRecord() { if (_configRecord == null) throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsectiongroup_when_not_attached); } public void Add(string name, ConfigurationSection section) { VerifyIsAttachedToConfigRecord(); _configRecord.AddConfigurationSection(_configSectionGroup.SectionGroupName, name, section); BaseAdd(name, name); } public void Clear() { VerifyIsAttachedToConfigRecord(); string[] array = BaseGetAllKeys(); foreach (string name in array) { Remove(name); } } public void CopyTo(ConfigurationSection[] array, int index) { if (array == null) throw new ArgumentNullException("array"); int count = Count; if (array.Length < count + index) throw new ArgumentOutOfRangeException("index"); int num = 0; int num2 = index; while (num < count) { array[num2] = Get(num); num++; num2++; } } public ConfigurationSection Get(int index) { return Get(GetKey(index)); } public ConfigurationSection Get(string name) { VerifyIsAttachedToConfigRecord(); if (string.IsNullOrEmpty(name)) throw ExceptionUtil.ParameterNullOrEmpty("name"); if (name.IndexOf('/') >= 0) return null; string configKey = BaseConfigurationRecord.CombineConfigKey(_configSectionGroup.SectionGroupName, name); return (ConfigurationSection)_configRecord.GetSection(configKey); } public override IEnumerator GetEnumerator() { int c = Count; for (int i = 0; i < c; i++) { yield return (object)this[i]; } } public string GetKey(int index) { return BaseGetKey(index); } public void Remove(string name) { VerifyIsAttachedToConfigRecord(); _configRecord.RemoveConfigurationSection(_configSectionGroup.SectionGroupName, name); string key = BaseConfigurationRecord.CombineConfigKey(_configSectionGroup.SectionGroupName, name); if (!_configRecord.SectionFactories.Contains(key)) BaseRemove(name); } public void RemoveAt(int index) { VerifyIsAttachedToConfigRecord(); Remove(GetKey(index)); } } }