TomlValue Class
Definition
Represents a scalar TOML value within a node tree - one of the eight TOML scalar kinds: a string, a 64-bit integer, a floating-point number, a Boolean, or one of the four date-time kinds.
public sealed class TomlValue : TomlNode
- Inheritance
-
TomlValue
- Inherited Members
- Extension Methods
Remarks
The stored kind is fixed at construction and reported by GetValueKind(). A scalar reads back as the CLR type that matches its kind; an integer additionally reads back as any fixed-width integer type through a checked conversion, and a float reads back as either double or float.
// Create wraps each TOML scalar kind - including the four native temporal kinds.
TomlValue text = TomlValue.Create("orders");
TomlValue offset = TomlValue.Create(new DateTimeOffset(2026, 1, 15, 8, 30, 0, TimeSpan.FromHours(10)));
TomlValue local = TomlValue.Create(new DateOnly(2026, 1, 15));
TomlValue time = TomlValue.Create(new TimeOnly(8, 30));
var date = local.GetValue<DateOnly>(); // 2026-01-15
Methods
Create(bool)
Creates a TomlValue holding the supplied Boolean value.
public static TomlValue Create(bool value)
Parameters
valueboolThe Boolean value.
Returns
- TomlValue
A new Boolean node.
Create(DateOnly)
Creates a TomlValue holding the supplied local date value.
public static TomlValue Create(DateOnly value)
Parameters
valueDateOnlyThe local date value.
Returns
- TomlValue
A new local-date node.
Create(DateTime)
Creates a TomlValue holding the supplied local date-time value.
public static TomlValue Create(DateTime value)
Parameters
valueDateTimeThe local date-time value.
Returns
- TomlValue
A new local-date-time node.
Create(DateTimeOffset)
Creates a TomlValue holding the supplied offset date-time value.
public static TomlValue Create(DateTimeOffset value)
Parameters
valueDateTimeOffsetThe offset date-time value.
Returns
- TomlValue
A new offset-date-time node.
Create(double)
Creates a TomlValue holding the supplied floating-point value.
public static TomlValue Create(double value)
Parameters
valuedoubleThe floating-point value.
Returns
- TomlValue
A new floating-point node.
Create(int)
Creates a TomlValue holding the supplied 32-bit integer.
public static TomlValue Create(int value)
Parameters
valueintThe integer value.
Returns
- TomlValue
A new integer-valued node.
Create(long)
Creates a TomlValue holding the supplied 64-bit integer.
public static TomlValue Create(long value)
Parameters
valuelongThe integer value.
Returns
- TomlValue
A new integer-valued node.
Create(string)
Creates a TomlValue holding the supplied string.
public static TomlValue Create(string value)
Parameters
valuestringThe string value.
Returns
- TomlValue
A new string-valued node.
Exceptions
- ArgumentNullException
Thrown when
valueis null.
Create(TimeOnly)
Creates a TomlValue holding the supplied local time value.
public static TomlValue Create(TimeOnly value)
Parameters
valueTimeOnlyThe local time value.
Returns
- TomlValue
A new local-time node.
DeepClone()
Creates a deep copy of this node and its entire subtree.
public override TomlNode DeepClone()
Returns
- TomlNode
An independent clone with no parent.
GetValueKind()
Gets the kind of value this node represents.
public override TomlValueKind GetValueKind()
Returns
- TomlValueKind
The TomlValueKind of this node.
GetValue<T>()
Returns the scalar value converted to the requested type.
public T GetValue<T>()
Returns
- T
The converted scalar value.
Type Parameters
TThe type to convert the scalar value to.
Exceptions
- InvalidOperationException
Thrown when the stored value cannot be converted to
T.
ToString()
Returns a string representation of this node.
public override string ToString()
Returns
- string
A textual rendering of this node.
TryGetValue<T>(out T)
Attempts to convert the scalar value to the requested type.
public bool TryGetValue<T>(out T value)
Parameters
valueTWhen this method returns true, the converted value; otherwise the default value of
T.
Returns
Type Parameters
TThe type to convert the scalar value to.
Remarks
A string value converts to string; an integer value converts to any fixed-width integer type through a checked conversion that fails when the value is out of range; a float value converts to double or float; a Boolean value converts to bool; and each date-time kind converts to its matching CLR type. Any other combination of stored kind and requested type yields false.
WriteTo(Utf8TomlWriter)
Writes the normalized TOML encoding of this node to the supplied writer.
public override void WriteTo(Utf8TomlWriter writer)
Parameters
writerUtf8TomlWriterThe destination writer.
Remarks
Because a TOML document's root must be a table, the writer produces a document only when the outermost node is a TomlObject. Writing a node whose root is a scalar or array yields no document; use ToUtf8Bytes() to obtain the normalized bytes, which validates the table-root rule.
Exceptions
- TomlSerializationException
Thrown when the subtree rooted at this node contains a null entry, which has no TOML representation.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |