How to check object is null or empty in C#.NET 3.5?

后端 未结 7 1082
悲哀的现实
悲哀的现实 2021-02-15 18:26

If objects contains null or empty then how to validate or check the condition for the same?

How to bool check whether object obj is null or

7条回答
  •  孤独总比滥情好
    2021-02-15 19:19

    The problem that you are running into is that your object is of type, well, object. In order to evaluate it with string.IsNullOrEmpty, you should pass your object in with the cast to (string)

    like so:

    static void Main(string[] args)
    {
        object obj = null;
    
        double d = Convert.ToDouble(string.IsNullOrEmpty((string)obj) ? 0.0 : obj);
        Console.WriteLine(d.ToString());
    }
    

    This will work fine since you are not explicitly calling .ToString on your (nonexistent) object.

提交回复
热议问题