C# equivalent of the IsNull() function in SQL Server

前端 未结 10 2103
太阳男子
太阳男子 2020-12-12 21:53

In SQL Server you can use the IsNull() function to check if a value is null, and if it is, return another value. Now I am wondering if there is anything similar

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 21:53

    This is meant half as a joke, since the question is kinda silly.

    public static bool IsNull (this System.Object o)
    {
       return (o == null);
    }
    

    This is an extension method, however it extends System.Object, so every object you use now has an IsNull() method.

    Then you can save tons of code by doing:

    if (foo.IsNull())
    

    instead of the super lame:

    if (foo == null)
    

提交回复
热议问题