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

后端 未结 7 1126
悲哀的现实
悲哀的现实 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条回答
  •  猫巷女王i
    2021-02-15 19:07

    You shouldn't be a bit surprised that you get a NullReferenceException with this code. The offending part is

    obj.ToString()
    

    If you wrote

    object obj = null;
    string s = obj.ToString();
    

    you would expect a NullReferenceException. Since the call to ToString occurs before the call to string.IsNullOrEmpty, the exception is thrown before there is a check for a null or empty string.

提交回复
热议问题