System.Runtime.CompilerServices.NullableAttribute
Assembly:
Defined as a compiler/embedded attribute (no dedicated public assembly; typically emitted or embedded by the compiler/Roslyn)
Namespace: System.Runtime.CompilerServices
Type: internal sealed class
Base: System.Attribute
Summary:
This attribute is a compiler-generated attribute used to convey nullable-reference-type information in metadata. It carries an array of byte flags (NullableFlags) that encode nullability annotations for the target (type, property, field, parameter, return value, generic parameter, etc.). It is marked with Microsoft.CodeAnalysis.Embedded so the compiler can embed it into assemblies for nullable metadata compatibility even when the consumer assembly does not itself reference a separate NullableAttribute type. This attribute is intended for compiler/runtime/analysis consumption — mods generally do not need to create or rely on it directly.
Fields
public readonly byte[] NullableFlags
Holds the raw nullability flags emitted by the compiler. Each byte represents nullability information for a type or type argument in the annotated target. The exact encoding is compiler-defined (commonly small integers representing oblivious/annotated/not-annotated states), so code should not make assumptions about numeric values beyond using compiler/runtime documentation when necessary.
Properties
- (none)
This type exposes no properties beyond its public readonly field.
Constructors
-
public NullableAttribute(byte P_0)
Creates the attribute with a single-byte flag. The constructor initializes NullableFlags to an array of length 1 containing the provided byte. -
public NullableAttribute(byte[] P_0)
Creates the attribute with an array of flags. The constructor initializes NullableFlags to the provided array reference.
Methods
- (none)
This attribute type does not declare methods beyond the constructors and the base Attribute members.
Usage Example
// Note: NullableAttribute is normally emitted by the C# compiler and is internal/embedded.
// Manually applying it is uncommon and not recommended for typical mod code.
// Example shows how the attribute might appear in metadata:
[System.Runtime.CompilerServices.Nullable(new byte[] { 1 })]
internal class ExampleWithNullableInfo
{
// ...
}
// Or, applied to a parameter/return (illustrative only):
[return: System.Runtime.CompilerServices.Nullable((byte)2)]
public string GetValue([System.Runtime.CompilerServices.Nullable((byte)1)] object input)
{
return input?.ToString();
}
Additional notes:
- The attribute targets include class, property, field, event, parameter, return value, and generic parameter (see AttributeUsage in source).
- Because it's compiler-generated and embedded, prefer relying on C# language nullable annotations (e.g., string?, #nullable directives) rather than manipulating this attribute directly in mods.