473,770 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ 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>(2 0);
Type type = j.GetType();
if (type.IsGeneric Type && type.GetGeneric TypeDefinition( ) ==
typeof(Nullable <>))
{
Console.WriteLi ne("j is Nullable");
}
else
Console.WriteLi ne("j is NOT Nullable");

Console.WriteLi ne("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 5679
"Joe Bloggs" <es***@rocketma il.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>(2 0);
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(ak a 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
5971
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 specific type the nullable type is.) The reason I'm trying this is because when I attempt to pass a nullable type value to a SqlCommand parameter and then attempt to execute it I the following error: "No mapping exists from object type...
1
1768
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 Convert.ChangetType((long) x, typeof(Nullable<long>)); throw an InvalidCastException. So I wonder, how could I, from the type information such as Type aType = typeof(Nullable<long>);
5
4681
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 I'll need to know if a type is nullable. -Josh
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
7
2344
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) http://msdn2.microsoft.com/en-us/library/ms366789.aspx
5
51672
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 DataColumn( "StartDate", typeof( DateTime? ) ) ); dtSearchFromData.Columns.Add( new DataColumn( "EndDate", typeof( System.Nullable<DateTime>) ) ); Any ideas?
8
10703
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
9565
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 same is no point to use the longer declaration as 2 it good enough to use decaration 1.
0
9617
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
9453
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
10254
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
10099
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...
1
10036
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,...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
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...
0
6710
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();...
2
3607
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.