C# Bug or Brain Teaser? Cast working only with Coalesce (??) Operator
        Posted  
        
            by Alex
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Alex
        
        
        
        Published on 2010-05-27T00:19:17Z
        Indexed on 
            2010/05/27
            0:31 UTC
        
        
        Read the original article
        Hit count: 272
        
This is very strange, maybe someone can explain what's happening, or this is a bug (though I tend to think that this is probably just something intricate about C#).
The following code throws the error "Cannot implicitly convert type 'uint?' to 'uint'.":
public void Test(UInt32? p)
{ 
    UInt32 x = p;
}
However, this code works without error:
public void Test(UInt32? p)
{
    UInt32 x = p ?? 1;
}
Huh? Why does this work? Why would the coalesce operator cause implicit conversion of UInt32? (nullable) to UInt32 (non-nullable), while the first error message says that there is no implicit conversion between those types?
© Stack Overflow or respective owner