Checking if an object is null in C#

前端 未结 18 1839
情深已故
情深已故 2020-11-28 02:12

I would like to prevent further processing on an object if it is null.

In the following code I check if the object is null by either:

if (!data.Equal         


        
18条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 02:20

    Whenever you are creating objects of class you have to check the whether the object is null or not using the below code.

    Example: object1 is object of class

    void myFunction(object1)
    {
      if(object1!=null)
      {
         object1.value1 //If we miss the null check then here we get the Null Reference exception
      }
    }
    

提交回复
热议问题