IniSection Class
Definition
- Namespace
- Bodu.Text.Configuration
- Assembly
- Bodu.Text.Configuration.dll
- Package
- Bodu.Text.Configuration 1.0.0
- Source
- IniSection.cs
Represents a single [section] block in an INI-style configuration document, exposing its ordered entries and
providing O(1) key lookup with optional typed value conversion.
public sealed class IniSection
- Inheritance
-
IniSection
- Inherited Members
- Extension Methods
Examples
IniSection db = doc.GetSection("database")!;
// TryGetValue + typed accessors for optional keys.
int port = db.TryGetValue("port", out int parsed) ? parsed : 5432;
if (db.TryGetValue("password", out string? password))
Console.WriteLine($"Password length: {password.Length}");
// Walk the entries in source order.
foreach (IniEntry entry in db.Entries)
Console.WriteLine($"{entry.Key} = {entry.Value}");
Remarks
The global section (keys that appear before any named section header) is also represented as an IniSection whose Name is the empty string.
Key lookup uses the comparer implied by CaseSensitive at parse time.
Constructors
IniSection(string, IEnumerable<IniEntry>, bool)
Initializes a new instance of the IniSection class with the supplied name and entries. Use this constructor to build sections programmatically without first parsing configuration text.
public IniSection(string name, IEnumerable<IniEntry> entries, bool caseSensitiveKeys = false)
Parameters
namestringThe section name, or an empty string for the global section.
entriesIEnumerable<IniEntry>The ordered entries for this section. Duplicate keys are resolved by last-wins to mirror LastWins; supply a deduplicated sequence to preserve order exactly.
caseSensitiveKeysbooltrue to compare keys with ordinal case sensitivity; otherwise, false (the INI default) for ordinal case-insensitive comparison.
Exceptions
- ArgumentNullException
Thrown when
nameorentriesis null.- ArgumentException
Thrown when
entriescontains a null entry.
Properties
Entries
Gets the ordered, deduplicated entries in this section.
public IReadOnlyList<IniEntry> Entries { get; }
Property Value
- IReadOnlyList<IniEntry>
A read-only list of IniEntry instances in source order, with duplicates resolved according to the DuplicateKeyPolicy that was active during parsing.
this[string]
Gets the value associated with the specified key, or null if the key is absent.
public string? this[string key] { get; }
Parameters
keystringThe key to look up.
Property Value
LeadingComments
Gets the comments authored on lines preceding this section header, in source order.
public IReadOnlyList<IniComment> LeadingComments { get; }
Property Value
- IReadOnlyList<IniComment>
A read-only view onto the section's leading-comment list. The list is empty when no comments precede the section.
Name
Gets the name of this section.
public string Name { get; }
Property Value
- string
The section name as it appeared in the source, or an empty string for the global section.
Methods
AddEntry(IniEntry)
Adds the supplied entry to this section, replacing an existing entry with the same key.
public void AddEntry(IniEntry entry)
Parameters
entryIniEntryThe entry to add or replace.
Exceptions
- ArgumentNullException
entryis null.
AddLeadingComment(IniComment)
Appends a leading comment to this section.
public void AddLeadingComment(IniComment comment)
Parameters
commentIniCommentThe comment to append.
ClearEntries()
Removes every entry from this section.
public void ClearEntries()
ClearLeadingComments()
Removes every leading comment from this section.
public void ClearLeadingComments()
GetValue<T>(string)
Gets the value associated with the specified key as type T.
public T GetValue<T>(string key) where T : ISpanParsable<T>
Parameters
keystringThe key to look up.
Returns
- T
The parsed value.
Type Parameters
TThe target type. Must implement ISpanParsable<TSelf>.
Exceptions
- ArgumentNullException
Thrown when
keyis null.- KeyNotFoundException
Thrown when
keyis not present in this section.- FormatException
Thrown when the value string cannot be parsed as
T.
RemoveEntry(string)
Removes the entry with the specified key.
public bool RemoveEntry(string key)
Parameters
keystringThe key to remove.
Returns
Exceptions
- ArgumentNullException
keyis null.
SetEntry(string, string)
Sets the value for key, creating a new entry when no entry with that key exists.
public IniEntry SetEntry(string key, string value)
Parameters
Returns
- IniEntry
The entry that now holds
value.
Exceptions
- ArgumentNullException
keyorvalueis null.
SetLeadingComments(IEnumerable<IniComment>)
Replaces the leading comments of this section with the supplied sequence.
public void SetLeadingComments(IEnumerable<IniComment> comments)
Parameters
commentsIEnumerable<IniComment>The new sequence of leading comments.
Exceptions
- ArgumentNullException
commentsis null.
TryGetEntry(string, out IniEntry?)
Attempts to locate the entry with the specified key.
public bool TryGetEntry(string key, out IniEntry? entry)
Parameters
Returns
TryGetValue(string, out string?)
Gets the value associated with the specified key.
public bool TryGetValue(string key, out string? value)
Parameters
keystringThe key to look up.
valuestringWhen this method returns true, contains the string value; otherwise, null.
Returns
TryGetValue<T>(string, out T)
Attempts to get the value associated with the specified key as type T.
public bool TryGetValue<T>(string key, out T value) where T : ISpanParsable<T>
Parameters
keystringThe key to look up.
valueTWhen this method returns true, contains the parsed result; otherwise, the default value of
T.
Returns
Type Parameters
TThe target type. Must implement ISpanParsable<TSelf>.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |