gpt4 book ai didi

C#中检查null的语法糖,非常实用

转载 作者:我是一只小鸟 更新时间:2023-01-25 22:31:06 24 4
gpt4 key购买 nike

c#处理null的几个语法糖,非常实用。(尤其是文末Dictionary那个案例,记得收藏) 。

?? 如果左边是的null,那么返回右边的操作数,否则就返回左边的操作数,这个在给变量赋予默认值非常好用.

                          
                            int
                          
                          ? a = 
                          
                            null
                          
                          
                            ;

                          
                          
                            int
                          
                           b = a ?? -
                          
                            1
                          
                          
                            ;
Console.WriteLine(b);  
                          
                          
                            //
                          
                          
                             output: -1
                          
                        

  。

??= 当左边是null,那么就对左边的变量赋值成右边的 。

                          
                            int
                          
                          ? a = 
                          
                            null
                          
                          
                            ;
a 
                          
                          ??= -
                          
                            1
                          
                          
                            ;
Console.WriteLine(a);  
                          
                          
                            //
                          
                          
                             output: -1
                          
                        

  。

?. 当左边是null,那么不执行后面的操作,直接返回空,否则就返回实际操作的值.

                          
                            using
                          
                          
                             System;

                          
                          
                            public
                          
                          
                            class
                          
                          
                             C {
    
                          
                          
                            public
                          
                          
                            static
                          
                          
                            void
                          
                          
                             Main() {
        
                          
                          
                            string
                          
                           i = 
                          
                            null
                          
                          
                            ;
        
                          
                          
                            int
                          
                          ? length = i?
                          
                            .Length;
        Console.WriteLine(length 
                          
                          ?? -
                          
                            1
                          
                          ); 
                          
                            //
                          
                          
                            output: -1
                          
                          
                                }
}
                          
                        

  。

?[] 索引器操作,和上面的操作类似 。

                          
                            using
                          
                          
                             System;

                          
                          
                            public
                          
                          
                            class
                          
                          
                             C {
    
                          
                          
                            public
                          
                          
                            static
                          
                          
                            void
                          
                          
                             Main() {
        
                          
                          
                            string
                          
                          [] i = 
                          
                            null
                          
                          
                            ;
        
                          
                          
                            string
                          
                           result = i?[
                          
                            1
                          
                          
                            ];
        Console.WriteLine(result 
                          
                          ?? 
                          
                            "
                          
                          
                            null
                          
                          
                            "
                          
                          ); 
                          
                            //
                          
                          
                             output:null
                          
                          
                                }
}
                          
                        

注意,如果链式使用的过程中,只要前面运算中有一个是null,那么将直接返回null结果,不会继续计算。下面两个操作会有不同的结果.

                          
                            using
                          
                          
                             System;

                          
                          
                            public
                          
                          
                            class
                          
                          
                             C {
    
                          
                          
                            public
                          
                          
                            static
                          
                          
                            void
                          
                          
                             Main() {
        
                          
                          
                            string
                          
                          [] i = 
                          
                            null
                          
                          
                            ;
        Console.WriteLine(i
                          
                          ?[
                          
                            1
                          
                          ]?.Substring(
                          
                            0
                          
                          ).Length); 
                          
                            //
                          
                          
                            不弹错误
                          
                          
        Console.WriteLine((i?[
                          
                            1
                          
                          ]?.Substring(
                          
                            0
                          
                          )).Length) 
                          
                            //
                          
                          
                             System.NullReferenceException: Object reference not set to an instance of an object.
                          
                          
                                }
}
                          
                        

  。

一些操作 。

                          
                            //
                          
                          
                            参数给予默认值
                          
                          
                            if
                          
                          (x == 
                          
                            null
                          
                          ) x = 
                          
                            "
                          
                          
                            str
                          
                          
                            "
                          
                          
                            ;

                          
                          
                            //
                          
                          
                            替换
                          
                          
x ??= 
                          
                            "
                          
                          
                            str
                          
                          
                            "
                          
                          
                            ;



                          
                          
                            //
                          
                          
                            条件判断
                          
                          
                            string
                          
                          
                             x;

                          
                          
                            if
                          
                          (i<
                          
                            3
                          
                          
                            ) 
    x 
                          
                          =
                          
                             y;

                          
                          
                            else
                          
                          
                             
{  
    
                          
                          
                            if
                          
                          (z != 
                          
                            null
                          
                          ) x =
                          
                             z; 
    
                          
                          
                            else
                          
                           z = 
                          
                            "
                          
                          
                            notnull
                          
                          
                            "
                          
                          
                            ;
}

                          
                          
                            //
                          
                          
                            替换
                          
                          
                            var
                          
                           x = i < 
                          
                            3
                          
                           ? y : z ?? 
                          
                            "
                          
                          
                            notnull
                          
                          
                            "
                          
                          
                            //
                          
                          
                            防止对象为null的时候,依然执行代码
                          
                          
                            if
                          
                          (obj != 
                          
                            null
                          
                          
                            ) 
    obj.Act();

                          
                          
                            //
                          
                          
                            替换
                          
                          
obj?
                          
                            .Act();


                          
                          
                            //
                          
                          
                            Dictionary取值与赋值
                          
                          
                            string
                          
                          
                             result;

                          
                          
                            if
                          
                          
                            (dict.ContainKey(key))
{
    
                          
                          
                            if
                          
                          (dict[key] == 
                          
                            null
                          
                          ) result = 
                          
                            "
                          
                          
                            有结果为null
                          
                          
                            "
                          
                          
                            ;
    
                          
                          
                            else
                          
                           result =
                          
                             dict[key];
}

                          
                          
                            else
                          
                          
                             
    result 
                          
                          = 
                          
                            "
                          
                          
                            无结果为null
                          
                          
                            "
                          
                          
                            ;

                          
                          
                            //
                          
                          
                            替换
                          
                          
                            var
                          
                           result= dict.TryGetValue(key, 
                          
                            out
                          
                          
                            var
                          
                           value) ? value ?? 
                          
                            "
                          
                          
                            有结果为null
                          
                          
                            "
                          
                           : 
                          
                            "
                          
                          
                            无结果为null
                          
                          
                            "
                          
                          ;
                        

  。

最后此篇关于C#中检查null的语法糖,非常实用的文章就讲到这里了,如果你想了解更多关于C#中检查null的语法糖,非常实用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com