26

I have a simple struct that has limited use. The struct is created in a method that calls the data from the database. If there is no data returned from the database I want to be able to return a null, but Visual Studio complains, Cannot convert null to PackageName.StructName because it is a non-nullable value type.

How can I make it nullable?

Malfist
  • 31,179
  • 61
  • 182
  • 269

7 Answers7

23

You want to look into the Nullable<T> value type.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
13
public struct Something
{
    //...
}

public static Something GetSomethingSomehow()
{
    Something? data = MaybeGetSomethingFrom(theDatabase);
    bool questionMarkMeansNullable = (data == null);
    return data ?? Something.DefaultValue;
}
mqp
  • 70,359
  • 14
  • 95
  • 123
  • 1
    As far as I understand, this code does not return null as Malfist wants to. – Luis Filipe Sep 01 '10 at 10:19
  • IIRC, it was intended as an illustration of the use of nullable types, since the poster didn't seem to understand exactly what they were. – mqp Sep 01 '10 at 14:03
  • `default(Something)` may have been a better default return value in the event that no value exists. – Chris Kerekes Apr 12 '13 at 14:17
8

The definition for a Nullable<T> struct is:

struct Nullable<T>
{
    public bool HasValue;
    public T Value;
}

It is created in this manner:

Nullable<PackageName.StructName> nullableStruct = new Nullable<PackageName.StructName>(params);

You can shortcut this mess by simply typing:

PackageName.StructName? nullableStruct  = new PackageName.StructName(params);

See: MSDN

John Rasch
  • 62,489
  • 19
  • 106
  • 139
4

Nullable<T> is a wrapper class that creates a nullable version of the type T. You can also use the syntax T? (e.g. int?) to represent the nullable version of type T.

Orion Adrian
  • 19,053
  • 13
  • 51
  • 67
  • Calling `Nullable` a "wrapper class" is incorrect: is a `struct`, not a `class`, so it has value-type semantics, not reference-type semantics. It _does_ have some special _compiler magic_ though, in that even though `Nullable` is a `struct`, it cannot be used as a generic type argument when the generic type parameter has a `T : struct` constraint, nor can `Nullable` be used _as_ `T` in another `Nullable`, so `Nullable>` won't compile either. – Dai Jul 20 '21 at 21:41
3

Use the built-in shortcuts for the Nullable<T> struct, by simply adding ? to the declaration:

int? x = null;

if (x == null) { ... }

Just the same for any other type, struct, etc.

MyStruct? myNullableStruct = new MyStruct(params);
Levite
  • 17,263
  • 8
  • 50
  • 50
1

You could make something nullable for example like this:

// Create the nullable object.
int? value = new int?();

// Check for if the object is null.
if(value == null)
{
    // Your code goes here.
}
Jake
  • 181
  • 2
  • 15
0

You can use default as an alternative

public struct VelocityRange
{
    private double myLowerVelocityLimit;
    private double myUpperVelocityLimit;
}

VelocityRange velocityRange = default(VelocityRange);

  • The `default(T)` of a value-type `T` is its zero-byte representation, which may be significant, especially when dealing with integer value-types, for example `default(Int32) == 0` - for that reason I never recommend using `default` as a stand-in for "null". – Dai Jul 20 '21 at 21:44