Skip to main content

Variant

Variant It is a generic value container that OPC UA covers all OPC UA built-in data types. It is used to pass values SDK for reading, writing, and function calls.

Supporting

Public Attributes​

CategoryPropertyTypeAccessDescription
TypeDataTypeBuiltinTypeRBuilt-in type
IsArrayboolRIs an array?
ArrayLengthintRNumber of array elements (negative for scalars)
ValueobjectRBoxed representation of the current value (based on DataType automatic type selection)

Assignment (scalar)​

Variant Only parameterless constructors new Variant(); assignments use type setters:

using System;
using DarraOpcUa_Client;

using var v = new Variant();
v.Boolean = true; // Boolean
v.Byte = 255; // Byte
v.Int16 = 42; // Int16
v.Int32 = 42; // Int32
v.UInt32 = 42u; // UInt32
v.Int64 = 42L; // Int64
v.Float = 3.14f; // Float
v.Double = 3.14; // Double
v.String = "hello"; // String
v.DateTime = DateTime.UtcNow; // DateTime

A setter is unidirectional (only set); reading is done via the property As*.

Each assignment overwrites the entire Variant

Variant isSingle-value container, not a set of fields. Each line in the code block above replaces the value and type of the previous line, leaving only the last line (DateTime). To store multiple types, create multiple Variant:

using var vBool = new Variant();  vBool.Boolean = true;
using var vNum = new Variant(); vNum.Double = 3.14;

Assignment (QualifiedName / LocalizedText)​

v.SetQualifiedName(2, "Temp");      // or SetQualifiedName(QualifiedName)
v.SetLocalizedText("zh-CN", "温度"); // or SetLocalizedText(LocalizedText)

Assignment (Arrays)​

v.SetBooleanArray(new[] { true, false, true });
v.SetInt32Array (new[] { 1, 2, 3 });
v.SetUInt32Array (new[] { 1u, 2u, 3u });
v.SetInt64Array (new[] { 1L, 2L, 3L });
v.SetFloatArray (new[] { 1.0f, 2.0f, 3.0f });
v.SetDoubleArray (new[] { 1.0, 2.0, 3.0 });
v.SetStringArray (new[] { "a", "b", "c" });

Read​

bool?   b   = v.AsBoolean;
int? i32 = v.AsInt32;
uint? u32 = v.AsUInt32;
long? i64 = v.AsInt64;
double? d = v.AsDouble;
string s = v.AsString;
DateTime? t = v.AsDateTime;
QualifiedName? qn = v.AsQualifiedName;
LocalizedText? lt = v.AsLocalizedText;

// Array (returns null if the type does not match or if it is not an array)
bool[] ab = v.AsBooleanArray;
int[] ai = v.AsInt32Array;
uint[] au = v.AsUInt32Array;
long[] al = v.AsInt64Array;
float[] af = v.AsFloatArray;
double[] ad = v.AsDoubleArray;
string[] ast = v.AsStringArray;

Scalar read properties return a nullable type (bool? / int? / uint? / long? / double? / DateTime?); if there is a type mismatch or the value cannot be retrieved null, the property returns, without throwing an exception. AsString / AsQualifiedName / AsLocalizedText Failure also returns null. If none is available AsFloat, single-precision values are AsDouble downconverted.

Is AsDouble a double?

AsDouble It returns a double double?, but passing it directly to a formal double parameter will result in a compilation error (CS1503):

double d0 = dv.Value.AsDouble;                 // CS1503
double d1 = dv.Value.AsDouble ?? double.NaN; // OK
Math.Round(dv.Value.AsDouble ?? 0, 2); // OK

ToString​

Variant.ToString() Format DataType by:

DataTypeOutput Example
Double3.14
Stringhello
DateTime2026-04-25T12:00:00.0000000Z (ISO 8601)
Null<null>
Array[1, 2, 3, 4, ... (N terms)]

List only the first 4 elements of the array and include the total number of elements. Types for which values cannot be retrieved are downcast to <DataType>.

Memory Ownership​

Variant Implementation IDisposable: Elements that are constructed new Variant() by the program itself, as well as those returned by functions such ua.Call as [...], each hold their own memory, Must Dispose。

From the subscription callback, the instance obtained DataValue.Value isBorrow Note: They are managed SDK by; calling Dispose on the C# side will not release them—you must call Dispose on the outer object DataValue.

using var v = new Variant();
v.Int32 = 42;
ua.Write("ns=2;s=X", v);

ua.Write It will not be released by the caller; it will be using cleaned up by.

BuiltinType Complete List​

Null, Boolean, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64,
Float, Double, String, DateTime, Guid, ByteString, XmlElement,
NodeId, ExpandedNodeId, StatusCode, QualifiedName, LocalizedText,
ExtensionObject, DataValue, Variant, DiagnosticInfo

A number in the range 0..25; see the BuiltinType enumeration definition.

Lists are valuable.Type Complete Set; The "Assignment / Read" sections above list the current subset of types for which setters and getters are provided; the difference between the two (SByte, UInt16, UInt64, Guid /, etc ByteString .) currently does not have direct read/write access.

ExtensionObject (Complex Structure)​

OPC UA server-side custom Struct DataTypes are wrapped in ExtensionObjects. When DataType == BuiltinType.ExtensionObject occurs, decode with StructDecoder:

using System;
using DarraOpcUa_Client;

var dec = new StructDecoder(ua);
using var dv = ua.Read("ns=2;s=RobotPose");

var fields = dv.Value.DecodeStructured(dec); // Field Name → Value
if (fields != null && fields.TryGetValue("X", out var xObj))
Console.WriteLine($"x = {xObj}");

DecodeStructured(StructDecoder) Returns Dictionary<string, object>; if ExtensionObject not, return null. DecodeStructured<T>(StructDecoder) Map to the property/field of the same name T; if ExtensionObject not, return default.

The field value cannot be resolved at this time

The TypeId / Body ExtensionObject for cannot be retrieved at this time, so DecodeStructured currently returns a dictionary containing only diagnostic _status items—not the actual field table. As a result, attempts to retrieve TryGetValue using above will fail "X". For details, see Structure Decoding "Implementation Limitations."

Next Step​