Table of Contents

IniEntry Class

Definition

Namespace
Bodu.Text.Configuration
Assembly
Bodu.Text.Configuration.dll
Package
Bodu.Text.Configuration 1.0.0
Source
IniEntry.cs

Represents a single key/value entry within an IniSection. The key and value are immutable data; callers assemble documents programmatically through the owning IniSection and may edit comment trivia before writing the document back out.

public sealed class IniEntry
Inheritance
IniEntry
Inherited Members
Extension Methods

Examples

// Build a section programmatically and save it back to text.
var doc = new IniDocument();
IniSection db = doc.GetOrAddSection("database");
db.SetEntry("host", "localhost");
IniEntry port = db.SetEntry("port", "5432");
port.InlineComment = new IniComment('#', " default Postgres port");

// Key and Value are immutable; replace the value via SetEntry.
db.SetEntry("port", "5433");

ConfigurationDocument.Save(doc, "app.config");

Remarks

Key and Value are immutable once constructed: the owning section maintains a key-to-entry lookup that depends on the key, and the value forms the entry's data identity. To change a value, call SetEntry(string, string), which replaces the entry while preserving its trivia.

The comment trivia - InlineComment and the LeadingComments list - remains editable so that annotations can be attached when authoring a document. The LineNumber reflects parser-assigned trivia and is read-only.

Constructors

IniEntry(string, string)

Initializes a new instance of the IniEntry class.

public IniEntry(string key, string value)

Parameters

key string

The trimmed key name.

value string

The trimmed value string.

Exceptions

ArgumentNullException

Thrown when key or value is null.

IniEntry(string, string, int)

Initializes a new instance of the IniEntry class with a source line number.

public IniEntry(string key, string value, int lineNumber)

Parameters

key string

The trimmed key name.

value string

The trimmed value string.

lineNumber int

The 1-based source line at which this entry was authored, or 0 when the entry was constructed programmatically rather than parsed.

Exceptions

ArgumentNullException

Thrown when key or value is null.

IniEntry(string, string, int, IReadOnlyList<IniComment>?)

Initializes a new instance of the IniEntry class with source line and leading-comment trivia.

public IniEntry(string key, string value, int lineNumber, IReadOnlyList<IniComment>? leadingComments)

Parameters

key string

The trimmed key name.

value string

The trimmed value string.

lineNumber int

The 1-based source line, or 0 when constructed programmatically.

leadingComments IReadOnlyList<IniComment>

The comments authored on lines that precede this entry, in source order, or null when no comments precede the entry. The supplied sequence is copied; subsequent mutations of the input collection do not affect this entry.

Exceptions

ArgumentNullException

Thrown when key or value is null.

Properties

InlineComment

Gets or sets the comment captured on the same line as this entry, or null when no inline comment is present.

public IniComment? InlineComment { get; set; }

Property Value

IniComment?

The inline comment, or null.

Key

Gets the key name of this entry.

public string Key { get; }

Property Value

string

The trimmed key string as it appeared in the source.

LeadingComments

Gets the comments authored on lines preceding this entry, in source order.

public IReadOnlyList<IniComment> LeadingComments { get; }

Property Value

IReadOnlyList<IniComment>

A read-only view onto the entry's mutable leading-comment list. The list is empty when no comments precede the entry.

LineNumber

Gets the 1-based source line number at which this entry was authored, or 0 when the entry was constructed programmatically rather than parsed.

public int LineNumber { get; }

Property Value

int

A non-negative line number.

Value

Gets the raw string value of this entry.

public string Value { get; }

Property Value

string

The entry value as supplied at construction. Never null.

Methods

AddLeadingComment(IniComment)

Appends a leading comment to this entry.

public void AddLeadingComment(IniComment comment)

Parameters

comment IniComment

The comment to append.

ClearLeadingComments()

Removes every leading comment from this entry.

public void ClearLeadingComments()

GetValue<T>()

Parses Value as T using InvariantCulture.

public T GetValue<T>() where T : ISpanParsable<T>

Returns

T

The parsed value.

Type Parameters

T

The target type. Must implement ISpanParsable<TSelf>.

Exceptions

FormatException

Thrown when Value cannot be parsed as T.

SetLeadingComments(IEnumerable<IniComment>)

Replaces the leading comments of this entry 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.

TryGetValue<T>(out T)

Attempts to parse Value as T using InvariantCulture.

public bool TryGetValue<T>(out T value) where T : ISpanParsable<T>

Parameters

value T

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

Returns

bool

true when parsing succeeded; otherwise, false.

Type Parameters

T

The target type. Must implement ISpanParsable<TSelf>.

Applies to

ProductVersions
.NET8, 10