473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

This is from a book which must be wrong nullable types

Hello!

I'm reading in a book called Visual C# 2005. In the chapter about Generics
there ia a section about Nullable types.

Here is the text that isn't complete true I think. It says:
"You can also look at the value of a reference type using the Value
property. If HasValue is true, then
you are guarantee a non-null value for Value, but if HasValue is false,
that is, null has been assigned to the variable, then accessing Value will
result in an exception of type System.InvalidOperationException."

Here it says reference type but this is wrong because it's a value type like
the normal primitive types like int, double and also struct is a value type.

For example int? intNullable = null;
Here the variable intNullable is a value not a reference type like they are
claiming in the text.

Do you agree with me here?

//Tony
Jun 27 '08 #1
6 1444
Tony Johansson <jo*****************@telia.comwrote:
I'm reading in a book called Visual C# 2005. In the chapter about Generics
there ia a section about Nullable types.

Here is the text that isn't complete true I think. It says:
"You can also look at the value of a reference type using the Value
property. If HasValue is true, then
you are guarantee a non-null value for Value, but if HasValue is false,
that is, null has been assigned to the variable, then accessing Value will
result in an exception of type System.InvalidOperationException."

Here it says reference type but this is wrong because it's a value type like
the normal primitive types like int, double and also struct is a value type.

For example int? intNullable = null;
Here the variable intNullable is a value not a reference type like they are
claiming in the text.

Do you agree with me here?
Yes. Which book is it? I suggest you try to find an errata page for the
book - it may already be listed, and if it isn't you should contact the
author so they can correct it for a future printing.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
>Do you agree with me here?
>
Yes. Which book is it? I suggest you try to find an errata page for
the book - it may already be listed, and if it isn't you should
contact the author so they can correct it for a future printing.
Ditching any and all C# books which don't list "Jon Skeet" as the author
isn't a bad option either.

An' I bet he can come up with a convincing LINQ statement for that.
Jun 27 '08 #3
On Jun 15, 10:50*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Tony Johansson <johansson.anders...@telia.comwrote:
I'm reading in a book called *Visual C# 2005. In the chapter about Generics
there ia a section about Nullable types.
Here is the text that isn't complete true I think. It says:
"You can also look at the value of a reference type using the Value
property. If HasValue is true, then
*you are guarantee a non-null value for Value, but if HasValue is false,
that is, null has been assigned to the variable, then accessing Value will
result in an exception of type System.InvalidOperationException."
Here it says reference type but this is wrong because it's a value type like
the normal primitive types like int, double and also struct is a value type.
For example int? *intNullable = null;
Here the variable intNullable is a value not a reference type like they are
claiming in the text.
Do you agree with me here?

Yes. Which book is it? I suggest you try to find an errata page for the
book - it may already be listed, and if it isn't you should contact the
author so they can correct it for a future printing.
It isn't exactly "wrong". You can set a reference variable to be null,
as
we all know:

bool? bValue = null;
if (bValue.HasValue)
{
Console.WriteLine("Reference is not null");
}

int? iValue = null;
if (iValue.HasValue)
{
Console.WriteLine("Reference is not null");
}

Works fine. The problem is probably the text surrounding the block the
OP
gave us. For example, from the microsoft site, we have:

Using Nullable Types (C# Programming Guide)

Nullable types can represent all the values of an underlying type, and
an additional null value. Nullable types are declared in one of two
ways:

System.Nullable<Tvariable

-or-

T? variable

T is the underlying type of the nullable type. T can be any value type
including struct; it cannot be a reference type.

For an example of when you might use a nullable type, consider how an
ordinary Boolean variable can have two values: true and false. There
is no value that signifies "undefined". In many programming
applications, most notably database interactions, variables can exist
in an undefined state. For example, a field in a database may contain
the values true or false, but it may also contain no value at all.
Similarly, *reference types* can be set to null to indicate that they
are not initialized.

This disparity can create extra programming work, with additional
variables used to store state information, the use of special values,
and so on. The nullable type modifier enables C# to create value-type
variables that indicate an undefined value.

***************

My emphasis above between the * and * in the quoted text. The book is
*probably* wrong, but not certainly
so. I'd want to see the whole page before I cast aspersions.

Matt
Jun 27 '08 #4
Matt <ma********@sprynet.comwrote:
Here is the text that isn't complete true I think. It says:
"You can also look at the value of a reference type using the Value
property. If HasValue is true, then
*you are guarantee a non-null value for Value, but if HasValue is false,
that is, null has been assigned to the variable, then accessing Valuewill
result in an exception of type System.InvalidOperationException."
Here it says reference type but this is wrong because it's a value type like
the normal primitive types like int, double and also struct is a value type.
For example int? *intNullable = null;
Here the variable intNullable is a value not a reference type like they are
claiming in the text.
Do you agree with me here?
Yes. Which book is it? I suggest you try to find an errata page for the
book - it may already be listed, and if it isn't you should contact the
author so they can correct it for a future printing.
It isn't exactly "wrong".
I disagree. It's this bit which is wrong:

"You can also look at the value of a reference type using the Value
property."

While there are no doubt reference types with a property called Value,
as it stands the statement is incorrect as a generalisation. It clearly
means "nullable value type" instead of "reference type" - as written
it's just plain wrong.
You can set a reference variable to be null, as we all know:
bool? bValue = null;
if (bValue.HasValue)
{
Console.WriteLine("Reference is not null");
}

int? iValue = null;
if (iValue.HasValue)
{
Console.WriteLine("Reference is not null");
}
Neither of those are references. They are both value types. When used
like this, the null literal doesn't mean the null reference - it means
the null value for that value type.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #5
On Jun 16, 3:51*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Matt <matttel...@sprynet.comwrote:
Here is the text that isn't complete true I think. It says:
"You can also look at the value of a reference type using the Value
property. If HasValue is true, then
*you are guarantee a non-null value for Value, but if HasValue is false,
that is, null has been assigned to the variable, then accessing Value will
result in an exception of type System.InvalidOperationException."
Here it says reference type but this is wrong because it's a value type like
the normal primitive types like int, double and also struct is a value type.
For example int? *intNullable = null;
Here the variable intNullable is a value not a reference type like they are
claiming in the text.
Do you agree with me here?
Yes. Which book is it? I suggest you try to find an errata page for the
book - it may already be listed, and if it isn't you should contact the
author so they can correct it for a future printing.
It isn't exactly "wrong".

I disagree. It's this bit which is wrong:

"You can also look at the value of a reference type using the Value
property."

While there are no doubt reference types with a property called Value,
as it stands the statement is incorrect as a generalisation. It clearly
means "nullable value type" instead of "reference type" - as written
it's just plain wrong.
Good point, I was looking at the other half of it.

As usual, sir, I bow to your wisdom :)

Matt
>
You can set a reference variable to be null, as we all know:
* * * * * * bool? bValue = null;
* * * * * * if (bValue.HasValue)
* * * * * * {
* * * * * * * * Console.WriteLine("Reference is not null");
* * * * * * }
* * * * * * int? iValue = null;
* * * * * * if (iValue.HasValue)
* * * * * * {
* * * * * * * * Console.WriteLine("Reference is not null");
* * * * * * }

Neither of those are references. They are both value types. When used
like this, the null literal doesn't mean the null reference - it means
the null value for that value type.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com- Hide quoted text -

- Show quoted text -
Jun 27 '08 #6
Neither of those are references. They are both value types. When used
like this, the null literal doesn't mean the null reference - it means
the null value for that value type.
Which is syntactic sugar in the C# compiler for

default(T?)
or
new Nullable<T>()

If I had wanted to design support for the "T? nt = null;" syntax I probably
would have used an implicit conversion from object... but you can't define
conversions from types in the same inheritance tree. Course I have to look
at Reflector every time to verify that there isn't a conversion operator
handling "null".
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: pete | last post by:
I found it in the view source of a corporate website. <script Language="Javascript"> <!-- var keyMacro= ]; //--> </script>
33
by: Tony Toews | last post by:
Folks David Fenton does a fine job in helping folks in this newsgroup. I have learned from his postings and have enjoyed our discussions here. He belongs in this newsgroup more than many. ...
96
by: Karen Hill | last post by:
SELECT surgeries.*, animals.* FROM surgeries INNER JOIN animals ON .=. AND WHERE ((.=Date()) Or .=Date()); I'm trying to write a query that joins two table together, animals and surgeries...
3
by: Sakoulakis | last post by:
Hi, I was just reading about nullable types and I like that feature very much. What dissapointed me is that nullable string is not supported. Imagine a field (of type sting) in a database...
10
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in...
13
by: Scott Hembrough | last post by:
Hello. I have two snippets of code here that are very similar. One works, but the other doesn't. Can someone explain why? Snippet 1: Local "date" variable is set to nothing. Compiles fine,...
2
by: Joe Bloggs | last post by:
Hi, compiling the following code: public class App { static void Main() { int? x = 5; bool? i = null;
2
by: Tony Johansson | last post by:
Hello! These two declarations(1 and 2) are the same I assume. 1. System.Nullable<intnullable; 2. System.Nullable<intnullable = new System.Nullable<int(); So because these 1 and 2 are the...
3
by: Tony Johansson | last post by:
Hello! Is it possible to declare existing .net generics to have nullable types(for example type int? ) ? If the answer on the previous question is yes then I assume that you can also define...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.