My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.
C# CODE EXAMPLE created on Tuesday, June 15, 2010 permalink
How to set a nullable type to null in a ternary operator
If you try to set e.g. a nullable int to null in a ternary operator, you get the error "there is no implicit conversion between null and int". Instead of assigning it to "null" assign it to default(int?). You can read more about why here. Here is an example with Guid.
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string agencyGuid = "";
            Guid? guid = new Guid();

            //this compiles:
            if (String.IsNullOrEmpty(agencyGuid))
                guid = null;
            else
                guid = new Guid(agencyGuid);

            //this compiles:
            guid = String.IsNullOrEmpty(agencyGuid) ? new Guid() : new Guid(agencyGuid);

            //but this gives the error: "there is no implicit conversion between null and System.Guid"
            guid = String.IsNullOrEmpty(agencyGuid) ? null : new Guid(agencyGuid);

            //so you have to use default()
            guid = String.IsNullOrEmpty(agencyGuid) ? default(Guid?) : new Guid(agencyGuid);

            Console.WriteLine(guid.ToString());
            Console.ReadLine();
        }
    }
}
need markup?