Making a Non-nullable value type nullable
Asked Answered
A

7

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?

Adlay answered 27/2, 2009 at 18:32 Comment(0)
G
22

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

Granth answered 27/2, 2009 at 18:34 Comment(1)
I used this and it still complainsFurie
D
13
public struct Something
{
    //...
}

public static Something GetSomethingSomehow()
{
    Something? data = MaybeGetSomethingFrom(theDatabase);
    bool questionMarkMeansNullable = (data == null);
    return data ?? Something.DefaultValue;
}
Discover answered 27/2, 2009 at 18:37 Comment(3)
As far as I understand, this code does not return null as Malfist wants to.Paraguay
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.Discover
default(Something) may have been a better default return value in the event that no value exists.Interpellate
N
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

Nidanidaros answered 27/2, 2009 at 18:42 Comment(0)
G
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.

Girlfriend answered 27/2, 2009 at 18:38 Comment(1)
Calling Nullable<T> 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<T> 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<T> be used as T in another Nullable<T>, so Nullable<Nullable<Int32>> won't compile either.Quern
A
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);
Admiration answered 9/5, 2016 at 7:18 Comment(0)
J
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.
}
Jacobsohn answered 20/1, 2013 at 1:4 Comment(0)
A
0

You can use default as an alternative

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

VelocityRange velocityRange = default(VelocityRange);

Afteryears answered 16/5, 2016 at 5:48 Comment(1)
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".Quern

© 2022 - 2024 — McMap. All rights reserved.