473,785 Members | 2,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detection of nullable types...

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
bool isNullableStruc tInt = i is Nullable<int>; //true
bool isValueType = i is ValueType; // true

bool isNullableStruc t = i is Nullable<>; // doesn't compile b/c must
provide a struct
bool isNullableStruc tValueType = i is Nullable<ValueT ype>; // doesn't
compile because ValueType is not a struct

System.Type iType = i.GetType(); // returns System.Int32

I also tried reflecting through the code and walking through the
documentation, but there doesn't seem to be any method of detecting if
a variable is a nullable struct. Does anyone know how to do this?

Apr 28 '06 #1
3 2104
*Update*

Actually, the following test produces even stranger results and has
prompted me to give up on my quest... although I think this is a major
issue.

int i2 = 32;
bool i2isNullableStr uctInt = i is Nullable<int>; // true
bool i2isInt = i is int; // also true

Apr 28 '06 #2
"rubikzube* " <th************ @gmail.com> wrote:
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.
Either the variable is declared to be of type Nullable<T> (aka T?), or it
is stored in an object reference. The implementation of nullables in the
CLR is such that it is impossible to have a boxed nullable on the heap.
That is:

int? x = 4;
object o = x;
Console.WriteLi ne(o.GetType(). Name);

should result in System.Int32 being printed to the console. The reason is
that when the CLR tries to box an instance of Nullable<T>, it looks at the
value inside the nullable struct. If the value is null, it boxes to a null
reference. If it isn't null, it gets boxed as the inner value of type T.

So, given a reference to a (possibly boxed struct) object of unknown type,
it is impossible for it to be an instance of Nullable<T>.
I also tried reflecting through the code and walking through the
documentation, but there doesn't seem to be any method of detecting if
a variable is a nullable struct. Does anyone know how to do this?


By design (and because of a late DCR (design change request)) between .NET
2.0 Beta 2 and .NET RTM, boxed nullables cannot occur.

The rational is that a boxed null is unintuitive. For example, with Beta
2, the following code would print 'True' followed by 'False':

int? x = null;
Console.WriteLi ne(x == null);
object o = x;
Console.WriteLi ne(o == null);

This was unintuitive, because the o reference wasn't null, it was pointing
to a boxed Nullable<int> instance whose value was null. This caused too
much confusion, so the CLR was modified to ensure that any attempt to box
a Nullable<T> resulted either in a null reference or a boxed instance of
T.

-- Barry
Apr 28 '06 #3
Wow. That was an extremely lucid response. My only follow up would be
that while a boxed null is unintuitive, there are situations where it
would be nice to determine if a generic value type passed into a
function should be considered nullable, just for transitivity in code
if nothing else.

As it stands, there is no way to know.

Thanks, dude.

Apr 28 '06 #4

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
3645
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?
6
3845
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the database for the given ID. I could just leave two separate constructors, or i could just have the default one call the second with a "-1" as the ID. This means that the initialization of certain fields can be based on whether my ID is -1 (for lazy...
8
4856
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked one of these syntax formats to work; // if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
0
1407
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously crippled), I don't know. But it seems to me obvious that there should be some way to specify that DB fields which can be null should be mapped to appropriate Nullable(Of T) generic types. For fields there's a dropdown in the dataset designer...
8
10704
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
2
2523
by: jon.rea | last post by:
Please tell me there is a better way :-D .... public static bool IsNullableType(Type paramType) { return paramType.IsGenericType && paramType.GetGenericTypeDefinition() == typeof (Nullable<>); }
0
9643
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
10315
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...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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
6737
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();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4045
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.