Wednesday, January 25, 2006
Nullable Types and Null Coalescing
Whilst thinking about how i used DBNull in C# in the past, wanted to consider how to best use the C# 2.0 features to really improve how I manage it all. My discovery is that there are some very cool new features in the languages and that setting magic numbers (Int32.MinValue) and so on to represent nulls can be a thing of the past!!
C# 2.0 uses the "?" type modifier that can be applied to all value and reference types to provide a nullable type of the underlying value type.
So, i can't do:
int age = null; //people don't want to tell me their age
and so i had to do
int age = Int32.MinValue;
or
int age = -1;
Now, however, i can just write:
int? age = null;
So if someone doesn't supply an age, i don't need to pretent their -1 or 150 just to get away from the fact i can't say they didn't tell me. Very coo. Read more here.
Unfortunately, you still have to use DBNull.Value as there is no implicit conversion from a .Net nullable type to a SQL Server nullable type. So you can't take a .Net int? that is null and just save this as NULL into the database, say by passing it as the value to a parameter on a Command. It throws an exception saying that you haven't even passed the parameter - although you have! Change it to a non-null value or a DBNull.Value and it works fine.
I do find this a little irritating and wouldn't mind knowing the reason behind it. Fortunately i did find some code that can help with this here from Manoj.
static object GetDbValue<t>(Nullable<t> val) where T : struct
{
if (val.HasValue)
{
return val.Value;
}
else
{
return DBNull.Value;
}
}
So this can be called from each of your data classes.
BUT, we're not done yet. A very cool feature is the null coalescing operator which is coded as "??". It allows you to set the value of nullable type and return the first non-nullable value.
So :
int? oldSalary = null;
int? newSalary = 27000;
int? currentSalary = oldSalary ?? newSalary
... will set the currentSalary to 27000, as this person has just joined and has no oldSalary.
Even nicer is that you can chain a load of these together, a simple example as below:
int? oldSalary = null;
int? currentSalary = null;
int? newSalary = 27000;
int? workingSalary = oldSalary ?? currentSalary ?? newSalary;
In this case it evaluates from left to right and sets the nullable workingSalary variable to the first non null value that is found - otherwise the result will be set to null (if they are all null).
You can even provide a default constant value, in which case the instance variable to which the result is placed can be nullable or value type. So say we wanted to modify the above, we could do.
int? oldSalary = null;
int? currentSalary = null;
int workingSalary = oldSalary ?? currentSalary ?? 27000;
So that's it for now. Very cool stuff though.
C# 2.0 uses the "?" type modifier that can be applied to all value and reference types to provide a nullable type of the underlying value type.
So, i can't do:
int age = null; //people don't want to tell me their age
and so i had to do
int age = Int32.MinValue;
or
int age = -1;
Now, however, i can just write:
int? age = null;
So if someone doesn't supply an age, i don't need to pretent their -1 or 150 just to get away from the fact i can't say they didn't tell me. Very coo. Read more here.
Unfortunately, you still have to use DBNull.Value as there is no implicit conversion from a .Net nullable type to a SQL Server nullable type. So you can't take a .Net int? that is null and just save this as NULL into the database, say by passing it as the value to a parameter on a Command. It throws an exception saying that you haven't even passed the parameter - although you have! Change it to a non-null value or a DBNull.Value and it works fine.
I do find this a little irritating and wouldn't mind knowing the reason behind it. Fortunately i did find some code that can help with this here from Manoj.
static object GetDbValue<t>(Nullable<t> val) where T : struct
{
if (val.HasValue)
{
return val.Value;
}
else
{
return DBNull.Value;
}
}
So this can be called from each of your data classes.
BUT, we're not done yet. A very cool feature is the null coalescing operator which is coded as "??". It allows you to set the value of nullable type and return the first non-nullable value.
So :
int? oldSalary = null;
int? newSalary = 27000;
int? currentSalary = oldSalary ?? newSalary
... will set the currentSalary to 27000, as this person has just joined and has no oldSalary.
Even nicer is that you can chain a load of these together, a simple example as below:
int? oldSalary = null;
int? currentSalary = null;
int? newSalary = 27000;
int? workingSalary = oldSalary ?? currentSalary ?? newSalary;
In this case it evaluates from left to right and sets the nullable workingSalary variable to the first non null value that is found - otherwise the result will be set to null (if they are all null).
You can even provide a default constant value, in which case the instance variable to which the result is placed can be nullable or value type. So say we wanted to modify the above, we could do.
int? oldSalary = null;
int? currentSalary = null;
int workingSalary = oldSalary ?? currentSalary ?? 27000;
So that's it for now. Very cool stuff though.
http://stevenR2.com
... a brief history
Site XML Feed
www.flickr.com |
Posts By Date
Profiles
- my taghop.org web
- taghop linkblogs
- todo list
- who i know
- flickr photos
- delicious links
- evdb events
- my things
- odeo subscriptions
NeighBloggers
taghop
Also made in Scotland
- Tarmac (John Loudon MacAdam)
- McIntosh Coat (Charles McIntosh)
- James Clerk Maxwell
- David Livingstone
- Television (John Logie Baird)
- BBC (Lord Reith)
- Kelvin Temperatures (William Thomson)
- RADAR (Sir Robert Alexander Watson-Watt)
- Steam Engine (James Watt)
- Americanism (John Witherspoon)
- Telephone (Alexander Graham Bell)
- Penicillin (Sir Alexander Fleming)
- Patron Saint of Ireland (Saint Patrick)
- Charles Rennie Mackintosh
- Celtic FC (Jock Stein)
- Treasure Island (Robert Louis Stevenson)
- Paddle Steamer (William Symington)
- Encylopaedia Britannica (William Smellie)
- Las Palmas Observatory (Charles Piazzi Smyth)
- The Bank of England (William Paterson)
- Logarithms & the Decimal Point (John Napier)
- The World Cup (Sir Thomas Lipton)
- MoreOver.com (David Galbraith)
- Blackboard (James Pillans)
- Liverpool FC (Bill Shankly)
- Iron Plough (James Small)
- Robinson Crusoe (Alexander Selkirk)
- Helium (Sir William Ramsay)
- Sociology (Adam Ferguson)
- Harry Potter
- Landspeed Record (Richard Noble)
- Hot Blast Oven (James Beaumont Neilson)
- Coal-Gas Lighting (William Murdock)
- Prime Minister of Canada (Sir John Alexander MacDonald)
- The Bicycle (Kirkpatrick Macmillan)
- Reflecting Telescope (James Gregory)
- The World's Worst Poet (William Topaz McGonagall)
- Geology (James Hutton)
- Carnegie Mellon (Andrew Carnegie)
- Carnegie Institution (Andrew Carnegie)
- Grandfather of the United States (Robert Dinwiddie)
- Universal Standard Time (Sir Sandford Fleming)
- Latent Heat & Carbon Dioxide (Joseph Black)
- James Bond (Sean Connery)
- Rob Stewart
- Auld Lang Syne (Robert Burns)
- Billy Connolly
- Annie Lennox
- U.S. Navy (John Paul Jones)
- Chariots of Fire (Eric Henry Liddell)
- Cure for Scurvy (James Lind)
- Tea Bags (Sir Thomas Lipton)
- Vacuum flask (Sir James Dewar)
- Postage Stamp (James Chalmers)
- Clerk Cycle Gas Engine (Sir Dugald Clerk)
- Cure for Malaria (George Cleghorn)
- Cure for Malaria (George Cleghorn)
- Groundskeeper Willie
- Peter Pan (Sir James Barrie)
- Kaleidoscope (Sir David Brewster)
- Toronto Globe (George Brown)
- Sherlock Holmes (Sir Arthur Conan Doyle)
- Graham's Law (Thomas Graham)
- The Wind in the Willows (Kenneth Grahame)
Release 2.0
Release 1.0
What I know
I Read
Archives
- July 17, 2005
- July 18, 2005
- July 19, 2005
- July 20, 2005
- July 21, 2005
- July 22, 2005
- July 27, 2005
- July 28, 2005
- August 01, 2005
- August 02, 2005
- August 03, 2005
- August 04, 2005
- August 07, 2005
- August 08, 2005
- August 09, 2005
- August 10, 2005
- August 11, 2005
- August 12, 2005
- August 13, 2005
- August 17, 2005
- August 19, 2005
- August 22, 2005
- August 24, 2005
- August 25, 2005
- August 27, 2005
- August 29, 2005
- August 30, 2005
- September 01, 2005
- September 02, 2005
- September 03, 2005
- September 04, 2005
- September 21, 2005
- September 22, 2005
- September 23, 2005
- September 30, 2005
- October 04, 2005
- October 06, 2005
- October 11, 2005
- October 14, 2005
- October 25, 2005
- October 27, 2005
- November 02, 2005
- November 08, 2005
- November 10, 2005
- November 12, 2005
- November 14, 2005
- November 16, 2005
- November 22, 2005
- December 02, 2005
- December 07, 2005
- December 23, 2005
- December 30, 2005
- January 02, 2006
- January 10, 2006
- January 11, 2006
- January 12, 2006
- January 14, 2006
- January 15, 2006
- January 16, 2006
- January 19, 2006
- January 20, 2006
- January 24, 2006
- January 25, 2006
- January 26, 2006
- January 30, 2006
- February 07, 2006
- February 08, 2006
- February 09, 2006
- February 20, 2006
- February 22, 2006
- February 23, 2006
- February 24, 2006
- February 27, 2006
- February 28, 2006
- March 01, 2006
- March 06, 2006
- March 08, 2006
- March 10, 2006
- March 13, 2006
- March 22, 2006
- March 24, 2006
- March 28, 2006
- March 29, 2006
- March 30, 2006
- March 31, 2006
- April 02, 2006
- April 06, 2006
- April 07, 2006
- April 13, 2006
- April 20, 2006
- April 26, 2006
- April 27, 2006
- April 28, 2006
- April 29, 2006
- April 30, 2006
- May 01, 2006
- May 02, 2006
- May 03, 2006
- May 04, 2006
- May 05, 2006
- May 07, 2006
- May 08, 2006
- May 10, 2006
- May 11, 2006
- May 15, 2006
- May 16, 2006
- June 02, 2006
- June 05, 2006
- June 06, 2006
- June 09, 2006
- June 11, 2006
- June 12, 2006
- June 13, 2006
- June 14, 2006
- June 20, 2006
- June 24, 2006
- June 26, 2006
- June 27, 2006
- June 29, 2006
- June 30, 2006
- July 01, 2006
- July 03, 2006
- July 08, 2006
- July 10, 2006
- July 12, 2006
- July 13, 2006
- July 25, 2006
- July 28, 2006
- August 01, 2006
- August 02, 2006
- August 05, 2006
- August 07, 2006
- August 08, 2006
- August 15, 2006
- August 22, 2006
- August 24, 2006
- August 27, 2006
- September 06, 2006
- September 07, 2006
- September 08, 2006
- September 11, 2006
- September 13, 2006
- September 14, 2006
- September 15, 2006
- September 21, 2006
- September 25, 2006
- October 02, 2006
- October 03, 2006
- October 25, 2006
- November 01, 2006
- November 10, 2006
- November 14, 2006
- November 15, 2006
- November 16, 2006
- November 17, 2006
- November 18, 2006
- November 20, 2006
- November 21, 2006
- November 29, 2006
- November 30, 2006
- December 08, 2006
- December 09, 2006