Nullable types – what does a null value represent?

B

beginwithl

hi


1) What does null value represent in nullable types – an unknown value
or non-existing/unassigned/undefined ( here I assume non-existing/
unassigned/undefined essentially mean the same thing ) value?

2) I understand nullable types are useful when dealing with databases,
but are they also useful in some other situation?

* One example where they might prove useful would be if you wanted a
variable to have no value due to not yet knowing what value the
variable should hold.


3) Nullable types have a property named Value, which basically gets a
value which nullable variable holds. But why would you ever need to
use Value property instead of “directly” using nullable variable?



thank you
 
P

Pavel Minaev

hi

1) What does null value represent in nullable types – an unknown value
or non-existing/unassigned/undefined ( here I assume non-existing/
unassigned/undefined essentially mean the same thing ) value?

In general, it represents whatever you want you to represent - it's
just a special marker value. For bools in particular, it represents
"unknown", as evident from the truth table for operators && and || on
nullable bools:

true && null == null
false && null == false
true || null == true
false || null = null

the above only makes sense if you substitute "null" with "unknown".
2) I understand nullable types are useful when dealing with databases,
but are they also useful in some other situation?

Yes. Any time you want to extend the value domain of an existing type
with one extra special distinct marker value. Most often it means
"unknown" or "not calculated yet"; sometimes, especially as a return
type of a function, it indicates some sort of failure (e.g. I could
and use null to said:
3) Nullable types have a property named Value, which basically gets a
value which nullable variable holds. But why would you ever need to
use Value property instead of “directly” using nullable variable?

Using the value property is exactly the same as casting the value of a
nullable type to a corresponding non-nullable type. In practice,
however, the latter is just C# syntactic sugar for the former. In
other words, you can consider HasValue and Value properties of
Nullable<T> struct as implementation details of nullable types, which
are not really needed in C# (but may be needed in other .NET languages
- there's no requirement to have syntactic sugar for nullable types in
general). Whether to use them or not is up to you - I generally prefer
comparison to null over HasValue because it is shorter, but I do use
Value sometimes, because writing a cast requires me to explicitly
spell out the type of the value that I want to get, which can be
lengthy (sometimes I wish there was a shortcat unary operator to cast
Nullable<T> to T - sort of like ?? without the default value).
 
G

Göran Andersson

beginwithl said:
1) What does null value represent in nullable types – an unknown value
or non-existing/unassigned/undefined ( here I assume non-existing/
unassigned/undefined essentially mean the same thing ) value?

It simply represents that there is no value. The reason behind this is
beyond the nullable type, you would have to look at how it's used to
determine that.
2) I understand nullable types are useful when dealing with databases,
but are they also useful in some other situation?

* One example where they might prove useful would be if you wanted a
variable to have no value due to not yet knowing what value the
variable should hold.

They are for example useful for avoiding magic values. For example:

int id = -1; // negative one means no value
if (Request.Form["id"] != null) {
id = int.Parse(Request.Form["id"]);
}
// compare to negative one to determine if there is a value
if (id != -1) {
LabelLoggedIn.Text = "Logged in";
}

This is clearer using a nullable type, it doesn't need extra comments to
document a magic value:

int? id = null;
if (Request.Form["id"] != null) {
id = int.Parse(Request.Form["id"]);
}
if (id.HasValue) {
LabelLoggedIn.Text = "Logged in";
}
3) Nullable types have a property named Value, which basically gets a
value which nullable variable holds. But why would you ever need to
use Value property instead of “directly” using nullable variable?

Sometimes you need the non-nullable type, and the compiler can't
determine that this is what you need.

Here's an example:

DateTime? date = DateTime.Now;
string displayDate = date.ToString("yyyy-MM-dd"); // fails
string displayDate = date.Value.ToString("yyyy-MM-dd"); // works
string displayDate = ((DateTime)date).ToString("yyyy-MM-dd"); // works

Casting the nullable value also works, but it's a bit more clumsy.
 
A

Arto Viitanen

beginwithl said:
hi


2) I understand nullable types are useful when dealing with databases,
but are they also useful in some other situation?

I have used mainly Nullable<bool>. They are quite handy when showing
check buttons and you really need the user to answer. So first, when the
user has not clicked the button, the value is null. Then he clicks it,
will come true, and then false. With only bool, you have to put the
check button to some value, so the user can skip the answer.
 
C

Cor Ligthert[MVP]

The use of the word null is the same as in the past often used High-Values,
it is simple an unassigned address.

It represent a fysical value which has nothing to do with any other value
not fffffffff or 0000000.

Therefore as you read in the computer world for null simple unassigned then
it is much easier to deal with it.

Cor


hi


1) What does null value represent in nullable types – an unknown value
or non-existing/unassigned/undefined ( here I assume non-existing/
unassigned/undefined essentially mean the same thing ) value?

2) I understand nullable types are useful when dealing with databases,
but are they also useful in some other situation?

* One example where they might prove useful would be if you wanted a
variable to have no value due to not yet knowing what value the
variable should hold.


3) Nullable types have a property named Value, which basically gets a
value which nullable variable holds. But why would you ever need to
use Value property instead of “directly” using nullable variable?



thank you
 
M

Michael C

Pavel Minaev said:
spell out the type of the value that I want to get, which can be
lengthy (sometimes I wish there was a shortcat unary operator to cast
Nullable<T> to T - sort of like ?? without the default value).

..Value is pretty short :)
 
B

beginwithl

hi

* I assume any value type ( thus any structure ) can be made into
nullable type?


Sometimes you need the non-nullable type, and the compiler can't
determine that this is what you need.

Here's an example:

DateTime? date = DateTime.Now;
string displayDate = date.ToString("yyyy-MM-dd"); // fails

* Why would the above assignment fail? Doesn’t date.ToString() convert
value of nullable type to its string representation?

Casting the nullable value also works, but it's a bit more clumsy.

Say we define a variable "int? V = 100;". Are there any other “unusual
situations” ( besides the one above ) where explicit cast of variable
V is necessary, but it wouldn’t be necessary if V was actually of type
int?


thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top