Member Access C # major operators in the

    During development, I met a member of the wording of one kind of null conditions of access, beginning not understand, after a special investigation of Microsoft's official documentation, the following specific content:

    Three members of the three forms of access

        (1) the XY : member access.

        (2) ? The X-.y : null member access conditions. If the left operand of the calculation result  nullis returned  null.

        (. 3) X [Y]? : Null condition or type of array element indexed access. If the left operand of the calculation result  nullis returned  null.  

    Wherein, I introduced at the second and third, x .y and x [y] syntax:?? Null condition operators are available in C # 6 and later, only when the calculated number of non-null operation result when, null conditional operator until the access member  ?. or element access  ?[] operation is applied to its operands. If the calculation result for the operand  null, the result is applied to the operator  nullNull conditions for member access operator  ?. , also known as Elvis operator. More

 

       Specific examples:

  

ConsoleApp_Test namespace 
{ 
    the delegate NumChange int (n-int); // define a delegate 
    class Program 
    { 
        static int NUM = 10; 
        public static int AddNum (n-int) 
        { 
            NUM = n-+; 
            return NUM; 
        } 


        static void the Main (String [] args ) 
        { 
            // delegate is null 
            NumChange nc = new new NumChange (AddNum); 
            nc = null; 
            ? int the Result = nc .Invoke (1);? 

            // delegate is not null 
            NumChange nc1 = new new NumChange (AddNum); 
            int ?? result1 = nc1 .Invoke (1 );
 
            // output
            Console.WriteLine (string.Format ( "first result: {0}, the second result: {}. 1", Result, RESULT1)); 


            the Console.ReadKey (); 
        } 
     
    } 
}

  Summary: Null condition operators are available in C # 6 and later, mainly used in the commission of an empty judgment, to ensure thread safety.

  And that 's share content, hope to help you!

    

 

Guess you like

Origin www.cnblogs.com/cjygrow/p/10993917.html