Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for initializing a nullable integer to default value?

Tags:

c#

nullable

I ran into some code which is setting a nullable integer like so:

int? xPosition = new int?();

This was an unfamiliar syntax for me. I would've expected either of these forms:

int? xPosition = null;
int? xPosition = default(int?);

Are there any functional differences between these three variable declarations?

like image 648
Sean Anderson Avatar asked Sep 07 '16 17:09

Sean Anderson


1 Answers

No functional difference. All three must instantiate an int?, and then since the default is HasValue == false, none of them require a subsequent member assignment.

like image 188
Ben Voigt Avatar answered Sep 28 '22 21:09

Ben Voigt