What's the difference between an option type and a nullable type?
Posted
by
Peter Olson
on Stack Overflow
See other posts from Stack Overflow
or by Peter Olson
Published on 2012-06-21T21:11:23Z
Indexed on
2012/06/22
15:16 UTC
Read the original article
Hit count: 315
In F# mantra there seems to be a visceral avoidance of null
, Nullable<T>
and its ilk. In exchange, we are supposed to instead use option types. To be honest, I don't really see the difference.
My understanding of the F# option type is that it allows you to specify a type which can contain any of its normal values, or
None
. For example, anOption<int>
allows all of the values that anint
can have, in addition toNone
.My understanding of the C# nullable types is that it allows you to specify a type which can contain any of its normal values, or
null
. For example, aNullable<int>
a.k.aint?
allows all of the values that anint
can have, in addition tonull
.
What's the difference? Do some vocabulary replacement with Nullable
and Option
, null
and None
, and you basically have the same thing. What's all the fuss over null
about?
© Stack Overflow or respective owner