Oliver Dixon
Oliver Dixon

Reputation: 7405

C# setting object as null

I want to set an object as null so I can 'consume' it of sorts. In Java we have this.

//In some function/object
Vector3 position = new Vector3();

//In some loop.
if(position != null){
    consumePositionAsForce(position);
    position = null;
}

I'm aware in C# that one has to 'Box' the and 'Unbox' the object if you are using primitive types buut I cannot find any documentation on nullable value types.

I'm trying to do the same in C# but I'm getting an errors/warning about type cast. As in I cannot set Vector3 = null.

Upvotes: 1

Views: 9699

Answers (6)

Pieter Jordaan
Pieter Jordaan

Reputation: 36

Vector3 is a struct and therefore is not nullable or disposable. You can either use

Vector3? position = null;

Or you can change it like this:

 class Program
{
    static void Main(string[] args)
    {
        using (Vector3 position = new Vector3())
        {
            //In some loop
           consumePositionAsForce(position);
        }
    }
}

struct Vector3 : IDisposable
{
    //Whatever you want to do here
}

The struct is now disposable so that you can use it in a using statement. This will kill the object after use. This is better than nulls since you don't over complicate things and you don't have to worry about missing a null check or event an undisposed object in memory.

Upvotes: 0

Yacoub Massad
Yacoub Massad

Reputation: 27861

You can use nullable types to do this:

Vector3? vector = null;

And assign its value from some place:

position = new Vector3();

And then you can easily compare it to null as you would have compared a reference type object:

if(position != null) //or position.HasValue if you want
{
    //...
}

After you verify it is not null, to access the Vector3 value, you should use position.Value.

Upvotes: 7

Ryan
Ryan

Reputation: 827

You can't set a value type to null.

Since Vector3 is a struct (which is a value type) you won't be able to set it to null as is.

You could use a nullable type like:

Vector3? position = null;

but that will require casting it to Vector3 when you want to use it in a function that is looking for regular Vector3.

Upvotes: 1

Felipe Oriani
Felipe Oriani

Reputation: 38608

You can have nullable value types using a the Nullable<T> where T is a struct (primite types) or adding a ? after as a prefix of the type. With this you can set, for sample, a int, a Vector, or Vector3d structure as nullable, for sample:

Vector? vector2d = null;

Vector3d? vector3d = null;

When you have nullable types, you has two new properties, which is HasValue which returns a bool value indicating if there is a valid value for the object and Value which return the real value (for a int? return a int). You could use something like this:

// get a structure from a method which can return null
Vector3d? vector3d = GetVector();

// check if it has a value
if (vector3d.HasValue)
{
   // use Vector3d
   int x = vector3d.Value.X;
}

Actually, the Nullable<T> class tries to encapsulate the value types as a reference types to give the impression you can set null for a value type.

I think you know but I recommend you read more about boxing and unboxing.

Upvotes: 5

Jack K
Jack K

Reputation: 71

Can you declare it as a nullable Vector3 (Vector3?)?

Vector3? position = null;

That'd be my first suggestion. Alternatively, you could set it to Vector3.Zero, but I don't really like that idea.

I'm fairly certain Vector3 is a value type, not a reference type, so you can't assign null to it without explicitly declaring it as a nullable Vector3.

Upvotes: 5

vincentp
vincentp

Reputation: 1433

Use Vector3? (nullable Vector3) instead of Vector3.

Upvotes: 4

Related Questions