473,407 Members | 2,359 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,407 software developers and data experts.

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

bool isNullableStruct = i is Nullable<>; // doesn't compile b/c must
provide a struct
bool isNullableStructValueType = i is Nullable<ValueType>; // 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 2081
*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 i2isNullableStructInt = 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.WriteLine(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.WriteLine(x == null);
object o = x;
Console.WriteLine(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
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...
6
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...
8
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...
0
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...
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.