操作有点不同

Ale*_*dre 0 c# nullable

class Program {

        static bool? a = null;
        static bool b = false;

        static void Main( string[] args ) {
            //1
            if( a!=null ) {
                b = (bool) a;
            }
            //2
            if( a!=null && (b=(bool) a) ) { }
        }
    }
Run Code Online (Sandbox Code Playgroud)

案例#1和案例#2之间有什么区别吗?

Ode*_*ded 6

就价值而言b,它们在功能上是相同的.

但是,既然你使用了nullables,你应该使用它们的功能:

if(a.HasValue)
   b = a.Value;
Run Code Online (Sandbox Code Playgroud)