Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Nullable type to string function

I just answered a thread where somebody wanted to know how to take any nullable type, check if it's value is null, and depending on that either return String.Empty or ToString. Here's how you do that:

    public static string ConvertNullableToString(T? value) where T : struct

    {

        return value.ToString() ?? String .Empty;

    }

 

     int ? value1 = null ;

     int ? value2 = 9;

 

     string stringValue1 = ConvertNullableToString< int >(value1);

     string stringValue2 = ConvertNullableToString< int >(value2);

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Monday, February 13, 2006 3:40 PMby Ryan Olshan
Filed under:

Comments

XIII said:

Hi,

you could also do it like

return value ?? string.Empty;

Grz, Kris.
# February 14, 2006 10:29 AM

Ryan Olshan said:

Ah. Yes. The ?? operator escaped my head when I was creating the code. I just updated it with the ?? operator. Thanks.
# February 14, 2006 7:55 PM
New Comments to this post are disabled