Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Converting a nullable object to an integer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
19 Mar 2011CPOL 13.9K   4   6
Here is a slightly shorter version of your first conversion:object someValue = 1;int result = someValue as int? ?? -1;
Here is a slightly shorter version of your first conversion:
C#
object someValue = 1;
int result = someValue as int? ?? -1;

License

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


Written By
Web Developer
United States United States

  • Managing Your JavaScript Library in ASP.NET (if you work with ASP.net and you don't read that, you are dead to me).
  • Graduated summa cum laude with a BS in Computer Science.
  • Wrote some articles and some tips.
  • DDR ("New high score? What does that mean? Did I break it?"), ping pong, and volleyball enthusiast.
  • Software I have donated to (you should too):

Comments and Discussions

 
Questionalways get null Pin
taleofsixstrings27-Sep-13 17:38
taleofsixstrings27-Sep-13 17:38 
AnswerRe: always get null Pin
AspDotNetDev28-Sep-13 15:14
protectorAspDotNetDev28-Sep-13 15:14 
GeneralDon't confuse devs by using "int? ?? -1" instead use some pa... Pin
Kunal Chowdhury «IN»16-May-11 19:10
professionalKunal Chowdhury «IN»16-May-11 19:10 
GeneralRe: Good advice. The code I presented is little more than a nove... Pin
AspDotNetDev16-May-11 19:45
protectorAspDotNetDev16-May-11 19:45 
GeneralThanks for your improvement! I Like your input, and included... Pin
Eduard Keilholz19-Mar-11 5:37
Eduard Keilholz19-Mar-11 5:37 
GeneralRe: Cool. FYI, I shortened the code further (didn't need parens ... Pin
AspDotNetDev19-Mar-11 10:43
protectorAspDotNetDev19-Mar-11 10:43 

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.