473,769 Members | 6,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2021
On Sep 30, 9:13*am, "Tony Johansson" <t.johans...@lo gica.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...@SP AMgmail.comwrot e:
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<Tne ed 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*********@sp amgmail.comwrot e:
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
1832
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 the declaration, eg: int? age; I don't like that much, I think it would be much more consistent to use a new keyword, such as "nullable". But anyways...
12
3644
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 conversion from a ..Net value type to to a Sql Server data type rather than having to use null cheked and DBNull.Value ? Does anyone have some explanation as to why this is so tricky as to be included?
3
2104
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 that I got when I attempted to perform some simple tests. Nullable<int> i = 32; bool isNullableClass = i is Nullable; // false
2
5483
by: Joe Bloggs | last post by:
Hi, compiling the following code: public class App { static void Main() { int? x = 5; bool? i = null;
8
10702
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
2593
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 UserName etc) You might have an Add User page where some of the fields do not have a value entered by the user, so you can account for this by making these types nullable in the method above. Is this the right usage of nullable types? And what...
6
1471
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 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
3
2415
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 my own generics class having nullable types. //Tony
4
2191
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 have a nullable field in the struct. can anyone please help me out regarding this
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.