473,320 Members | 1,856 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,320 software developers and data experts.

nullable types is a struct ?

Hello!

Jon skeet answer this question in a previous mail for several days ago if
nullable type is a reference or a value type?

With the following answer.
It's a struct - otherwise there'd be relatively little value in having
it instead of having explicit access to the boxed types.

But what does the answer actually mean?

//Tony

Sep 30 '08 #1
8 2002
On Sep 30, 9:13*am, "Tony Johansson" <t.johans...@logica.comwrote:
Jon skeet answer this question in a previous mail for several days ago if
nullable type is a reference or a value type?
It's a value type.
With the following answer.
It's a struct - otherwise there'd be relatively little value in having
it instead of having explicit access to the boxed types.

But what does the answer actually mean?
It's still a struct, so there's no separate heap object created. In
other words, it's like this:

struct Nullable<Twhere T : struct
{
private T value;
private boolean hasValue;

// Properties, constructor etc
}

An alternative would have been to make a "wrapper class":

class Nullable<Twhere T : struct
{
private T value;
}

where you'd have a genuine null reference instead of a reference to an
instance. However, that then puts more pressure on the GC etc.

There are a few ways in which nullable types aren't like other
structs:
o The null value boxes to a null reference
o You can unbox from a boxed value of the non-nullable type, or from a
null reference
o You can't use it as the type argument for something with a "where
T : struct" constraint
o You can use null to compare/assign the null value of the nullable
type (i.e. hasValue = false)

Jon
Sep 30 '08 #2
System.Nullable is a ValueType. It is a generic class, so the reference to
its value is not boxed, the value is also stored as a ValueType. In
addition to holding the value it as a "bool HasValue" property that
indicates whether the value has been set or not.
Does that help?

Sep 30 '08 #3
On Sep 30, 9:30*am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
System.Nullable is a ValueType. *It is a generic class, so the reference to
its value is not boxed, the value is also stored as a ValueType.
Hang on a sec. Just to be pedantic:

System.Nullable is a static class.
System.Nullable<Tis a value type - a generic *struct* (not class).

Personally I think it's a shame that System.Nullable (the class) even
exists, but there we go...

Jon
Sep 30 '08 #4
>>
Hang on a sec. Just to be pedantic:

System.Nullable is a static class.
System.Nullable<Tis a value type - a generic *struct* (not class).
<<

You're pedantic, I'm too idle to write <T:-)
Sep 30 '08 #5
It makes a difference, though.

OK, maybe with List/List<Twe can guess - but Action & Action<Thave
very different meanings, for example. Likewise, IEnumerable and
IEnumerable<Tneed disambiguating. If I'm feeling lazy, I'll use the C#
syntax - i.e. List<>, Dictionary<,etc.

Marc
Sep 30 '08 #6
I'm not saying it doesn't matter. I am just saying that I often type a bit
vaguely when I think it's okay, and that the real meaning is easily inferred
based on what has been said previously. Personally I didn't know that
System.Nullable is a static class, I'm not pretending I knew, I've never
used it. I just meant System.Nullable<Tand was being lazy when I typed
:-)
Pete

Sep 30 '08 #7
On Tue, 30 Sep 2008 10:45:35 -0700, Peter Morris
<mr*********@spamgmail.comwrote:
I'm not saying it doesn't matter. I am just saying that I often type a
bit vaguely when I think it's okay, and that the real meaning is easily
inferred based on what has been said previously. Personally I didn't
know that System.Nullable is a static class, I'm not pretending I knew,
I've never used it. I just meant System.Nullable<Tand was being lazy
when I typed :-)
Yes, but you called it a class, when System.Nullable<Tis a struct.
There were two possible ways to interpret your statement, and neither
interpretation could be converted into a correct statement.

I empathize with the tempatation to be lazy, but programming is a
profession of intracacies and exactness. Heck, for that matter, most
professions are when done properly. But in programming it's particularly
important to be precise about what one says, because otherwise you may
wind up saying something completely different from what's true. As you
did in this particular case.

Barring being precise, one should at least be willing to accept a
correction without expressing defensiveness. :p

Pete
Sep 30 '08 #8
Yes, but you called it a class, when System.Nullable<Tis a struct.

That's definitely a habit of mine. I don't bother defining anything as a
struct so I am still in the habit of calling everything a class, even after
all these years.
I empathize with the tempatation to be lazy, but programming is a
profession of intracacies and exactness.
Yeah, but then there is the consideration that my answer was essentially "It
*is* stored as a value type and not boxed". I don't really want to have to
check the padding text of my posts for 100% accuracy, and I don't suppose I
ever will.
wind up saying something completely different from what's true.
The essence of what I said was true. The value will not be boxed. The text
relevant to the question was accurate. The rest I paid less attention to.

Barring being precise, one should at least be willing to accept a
correction without expressing defensiveness. :p
Here I will be defensive. My reply to Jon was light hearted and I can't see
how it could possibly be perceived otherwise. On the whole I don't mind
being corrected at all, if I am wrong about something I like to know.
Pete

Oct 1 '08 #9

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

Similar topics

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...
12
by: Steven Livingstone | last post by:
I've just blogged some stuff on Nullable types in net 2.0. http://stevenr2.blogspot.com/2006/01/nullable-types-and-null-coalescing.html Question however as to why you can't simply get an implcit...
3
by: rubikzube* | last post by:
Hi. I'm trying to write a snippet of code to detect if a variable is a nullable<t> struct, and I can't figure out the right syntax or if such a thing is even possible... Below are the results...
2
by: Joe Bloggs | last post by:
Hi, compiling the following code: public class App { static void Main() { int? x = 5; bool? i = null;
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
3
by: Mike P | last post by:
What are nullable types used for? The only use I can think of is where I have a method like this where some values being passed to it may be null : public int AddUser(int? UserRegion, string...
6
by: Tony Johansson | last post by:
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...
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...
4
by: James | last post by:
hello, can we have a nullable type field in the struct ? struct Employee { nullable<stringfirstName =null; } we can't initialize a field in a struct so is there any possibility that we can...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.