473,382 Members | 1,658 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,382 software developers and data experts.

typeof check for nullability

I am guessing there is a simple solution but given a type T, how can I check
for nullability?

how can I accomplish the following?

bool nullable = typeof(int).IsNullable; // false
bool nullable = typeof(int?).IsNullable; // true
bool nullable = typeof(string).IsNullable; // true

Thanks for any help.

--

Andrew Robinson
Jul 24 '06 #1
2 2319
Andrew Robinson <ne****@nospam.nospamwrote:
I am guessing there is a simple solution but given a type T, how can I check
for nullability?

how can I accomplish the following?

bool nullable = typeof(int).IsNullable; // false
bool nullable = typeof(int?).IsNullable; // true
bool nullable = typeof(string).IsNullable; // true
using System;

class Test
{
static void Main()
{
Console.WriteLine (IsNullable(typeof(int)));
Console.WriteLine (IsNullable(typeof(int?)));
Console.WriteLine (IsNullable(typeof(string)));
}

static bool IsNullable (Type t)
{
return !t.IsValueType ||
(t.IsGenericType &&
t.GetGenericTypeDefinition()==typeof(Nullable<>));
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 24 '06 #2
Thanks,

--

Andrew Robinson
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Andrew Robinson <ne****@nospam.nospamwrote:
>I am guessing there is a simple solution but given a type T, how can I
check
for nullability?

how can I accomplish the following?

bool nullable = typeof(int).IsNullable; // false
bool nullable = typeof(int?).IsNullable; // true
bool nullable = typeof(string).IsNullable; // true

using System;

class Test
{
static void Main()
{
Console.WriteLine (IsNullable(typeof(int)));
Console.WriteLine (IsNullable(typeof(int?)));
Console.WriteLine (IsNullable(typeof(string)));
}

static bool IsNullable (Type t)
{
return !t.IsValueType ||
(t.IsGenericType &&
t.GetGenericTypeDefinition()==typeof(Nullable<>));
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jul 25 '06 #3

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

Similar topics

3
by: TD | last post by:
This code doesn't work. Every posting I can find suggests that it should. If TypeOf controlname Is DTPicker then do something here End If I am using the Date Time Picker control and wish to...
4
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in...
3
by: Alberto | last post by:
Can somebody tell me why this typeof doesn't work? foreach (Control myControl in Controls) if (typeof(myControl) == "TextBox") ((TextBox)myControl).Text = string.Empty; Thank you very much
13
by: Jason Huang | last post by:
Hi, Would someone give me an example for the typeof() function? What's that for? Thanks for help. Jason
1
by: Brien King | last post by:
Ok, I have three classes (The example here is extremely simplified to illustrate the problem) like this: Public Class A Public Sub DoSomething(ByVal myClass) If TypeOf myClass IS A Then ' '...
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
4
by: EManning | last post by:
Using A2003. I've got an option group that has a number of check boxes. I have coding to clear the option group if the user wishes to cancel their choice. This coding also clears the rest of the...
11
by: -Lost | last post by:
I have this generic function (JavaScript newbie here, so don't think I am going to impress you): function blah() { var container = ''; for(var i = 0; i < arguments.length; i++) { container...
4
by: RP | last post by:
I have a member variable of a class of type "int", and while assigning value to it in a constructor, I want to check whether it is a null. I am using following code: if (NewMember.BPLNo == null)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.