Difference between "?" and "??" operators in C#

12. May 2008

Conventional Code:

   string f(string inputString)
    {
        if (inputString != null)
            return inputString;
        else
            return "";
    }

Better code with operator ?

    string f(string inputString)
    {
        return (inputString != null) ? inputString : "";
    }

Much better code with operator ??

    string f(string inputString)
    {
        return (inputString) ?? "";
    }

Better Coding

Comments

7/7/2010 7:24:01 PM #
This is the perfect blog for anyone who wants to know about this topic.  You know so much its almost hard to argue with you (not that I really would want...HaHa).  You definitely put a new spin on a subject thats been written about for years.  Great stuff, just great!

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading