Table of Contents

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

name string

The section name, or an empty string for the global section.

entries IEnumerable<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.

caseSensitiveKeys bool

true to compare keys with ordinal case sensitivity; otherwise, false (the INI default) for ordinal case-insensitive comparison.

Exceptions

ArgumentNullException

Thrown when name or entries is null.

ArgumentException

Thrown when entries contains 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

key string

The key to look up.

Property Value

string

The string value, or null when the key is not present.

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

entry IniEntry

The entry to add or replace.

Exceptions

ArgumentNullException

entry is null.

AddLeadingComment(IniComment)

Appends a leading comment to this section.

public void AddLeadingComment(IniComment comment)

Parameters

comment IniComment

The 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

key string

The key to look up.

Returns

T

The parsed value.

Type Parameters

T

The target type. Must implement ISpanParsable<TSelf>.

Exceptions

ArgumentNullException

Thrown when key is null.

KeyNotFoundException

Thrown when key is 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

key string

The key to remove.

Returns

bool

true when an entry was removed; otherwise, false.

Exceptions

ArgumentNullException

key is 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

key string

The key to set.

value string

The value to assign.

Returns

IniEntry

The entry that now holds value.

Exceptions

ArgumentNullException

key or value is null.

SetLeadingComments(IEnumerable<IniComment>)

Replaces the leading comments of this section with the supplied sequence.

public void SetLeadingComments(IEnumerable<IniComment> comments)

Parameters

comments IEnumerable<IniComment>

The new sequence of leading comments.

Exceptions

ArgumentNullException

comments is null.

TryGetEntry(string, out IniEntry?)

Attempts to locate the entry with the specified key.

public bool TryGetEntry(string key, out IniEntry? entry)

Parameters

key string

The key to look up.

entry IniEntry

When this method returns true, the matching entry.

Returns

bool

true when the key is present; otherwise, false.

TryGetValue(string, out string?)

Gets the value associated with the specified key.

public bool TryGetValue(string key, out string? value)

Parameters

key string

The key to look up.

value string

When this method returns true, contains the string value; otherwise, null.

Returns

bool

true when the key is present; otherwise, false.

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

key string

The key to look up.

value T

When this method returns true, contains the parsed result; otherwise, the default value of T.

Returns

bool

true when the key is present and its value was successfully parsed as T; otherwise, false.

Type Parameters

T

The target type. Must implement ISpanParsable<TSelf>.

Applies to

ProductVersions
.NET8, 10