473,496 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reflection and Nullable in .Net 2.0

Hi,

Can someone please kindly show me how to determine if a type (read value
type) is Nullable.

MSDN has this KB:
How to: Identify a Nullable Type (C# Programming Guide)
http://msdn2.microsoft.com/en-us/library/ms366789.aspx

however, using their code snippet, I couldn't get it to work:

public class MyClass
{
public static void Main()
{
Nullable<intj = new Nullable<int>(20);
Type type = j.GetType();
if (type.IsGenericType && type.GetGenericTypeDefinition() ==
typeof(Nullable<>))
{
Console.WriteLine("j is Nullable");
}
else
Console.WriteLine("j is NOT Nullable");

Console.WriteLine("Type of j is {0}", j.GetType());
}
}

The Output I got is:

j is NOT Nullable
Type of j is System.Int32

thus, is there no way to determine a nullable or an non-nullable value
types?

Thanks for your help in advanced.

Jul 13 '06 #1
1 5666
"Joe Bloggs" <es***@rocketmail.comwrote:
Can someone please kindly show me how to determine if a type (read value
type) is Nullable.
If you have the value in a variable of type object, you cannot. See
below for more details.
MSDN has this KB:
How to: Identify a Nullable Type (C# Programming Guide)
http://msdn2.microsoft.com/en-us/library/ms366789.aspx
The documentation on this page reads:

"Calling GetType on a Nullable type causes a boxing operation to be
performed when the type is implicitly converted to Object. Therefore
GetType always returns a Type object that represents the underlying
type, not the Nullable type."

Thus, the behaviour you're getting is as documented.
Nullable<intj = new Nullable<int>(20);
Type type = j.GetType();
Nullable<Tdoesn't override GetType(), so this method call ends up
boxing j. The way boxing nullable types works on the CLR is that null
values get turned into null references, while non-null inner values get
boxed into the inner type.

So, j, of type Nullable<int(aka int?), when boxed, is of type int
(i.e. System.Int32).
thus, is there no way to determine a nullable or an non-nullable value
types?
It is impossible to get a boxed nullable value in a variable of type
object. They do not occur. The boxed nullable value is either null or of
the inner type. Casting back to a nullable type performs the appropriate
null check automatically.

-- Barry

--
http://barrkel.blogspot.com/
Jul 13 '06 #2

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

Similar topics

4
5937
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what...
1
1754
by: Lloyd Dupont | last post by:
I have written a class which automatically fills up an object's field from a DbDataReader. I'm trying to do some data type conversion before assigning the fields. Unfortunately...
5
4650
by: Narshe | last post by:
How can I check if a type is nullable? If a type is created like bool? = null;, then how can you tell if the type is nullable as opposed to not? You have to cast from bool to bool? and such, so...
8
4837
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...
7
2333
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
5
51538
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
8
10690
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>{ ...
2
9539
by: Jay | last post by:
Is there a way to access the actual datatype of a property when looking at the PropertyInfo? For example: Property defined: public Nullable<DateTimeEndDate { get { return endDate; }
2
180
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...
0
7120
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
6991
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
7160
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
7196
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
7373
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...
0
5456
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,...
1
4897
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...
0
4583
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...
0
286
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...

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.