Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nullable var using implicit typing in c#?

Is there anyway to have the var be of a nullable type?

This implicitly types i as an int, but what if I want a nullable int?

var i = 0;

Why not support this:

var? i = 0;

like image 397
user37468 Avatar asked Feb 11 '09 15:02

user37468


2 Answers

var is typed implicitly by the expression or constant on the right hand side of the assignment. var in and of itself is not a type so Nullable<var> is not possible.

like image 80
Andrew Hare Avatar answered Oct 05 '22 08:10

Andrew Hare


Why support it? If that's what you mean, you should say var i = (int?)0; instead.

(Well, you should probably just say int? i = 0, but for more complicated types, etc.)

like image 38
mqp Avatar answered Oct 05 '22 09:10

mqp