Skip to main content

Struct Optional<T>

Represents an optional value of type T with an enable flag, allowing you to control whether the value should be considered active.

[Serializable]
public struct Optional<T>

Type Parameters​

NameTypeDescription
TThe type of the optional value.

Constructors​

Optional(T)​

Initializes a new instance of the Optional struct with the given value, enabling the optional by default.

public Optional(T initialValue)

Parameters​

NameTypeDescription
initialValueTThe initial value to store.

Properties​

Enabled​

Gets a value indicating whether this optional is enabled.

public bool Enabled { get; }

Property Value​

bool

Value​

Gets or sets the stored value.

public T Value { get; set; }

Property Value​

T

Methods​

IsValid()​

Checks whether the optional is enabled and contains a non-null value.

public bool IsValid()

Returns​

bool — True if enabled and the value is not null; otherwise, false.

SetEnable(bool)​

Sets the enabled state of the optional.

public void SetEnable(bool value)

Parameters​

NameTypeDescription
valueboolTrue to enable; false to disable.