Night Walker
Night Walker

Reputation: 21280

putting value to nullable variable

How is the best to do following Booking Address is a structure

BookingAddress? retval= null;

here comes my logic and ihave following

BookingAddress address = new BookingAddress(reelinfo.LineID, reelinfo.McID);
retval = address;

Is there more nice way to put the needed value to the retVal ?

Upvotes: 0

Views: 74

Answers (3)

Ran
Ran

Reputation: 6159

This is all you need:

return new BookingAddress(reelinfo.LineID, reelinfo.McID);

You don't really need the retval variable if you are not doing anything with it between assigining its value and returning it.

Upvotes: 1

CodesInChaos
CodesInChaos

Reputation: 108810

Why not simply:

BookingAddress? retval = new BookingAddress(reelinfo.LineID, reelinfo.McID);

A non nullable value type is implicitly convertible to the corresponding nullable value-type.

BookingAddress? retval= null;
if(...)
  retval = new BookingAddress(reelinfo.LineID, reelinfo.McID);

Upvotes: 3

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391416

What about just:

retval = new BookingAddress(...);

Upvotes: 2

Related Questions