Click here to Skip to main content
12,621,430 members (30,043 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

11.6K views
2 bookmarked
Posted

Converting a nullable object to an integer

, 19 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.

As of .NET 2.0, we have a built-in way[^] of converting a Nullable<T> object to an object of type T with a default::

int? a;
int b = a.GetValueOrDefault(-1);

So to converting an object to an integer would then be:

object someValue = null; // Can be anything
int convertedInteger = ((int?)someValue).GetValueOrDefault(-1);

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jonathan Cardy
Software Developer Repstor Ltd
United Kingdom United Kingdom
I am a Product Architect at Repstor.

Repstor custodian and Repstor Provisioning Engine provide case management and provisioning for SharePoint.

Repstor affinity provides uninterrupted access to content systems, like SharePoint through the familiar interface of Microsoft Outlook.

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 5 thanks a lot! Pin
DRiVe-X-S23-May-11 20:31
memberDRiVe-X-S23-May-11 20:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161128.1 | Last Updated 19 Mar 2011
Article Copyright 2011 by Jonathan Cardy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid