Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#.NET check if the object is null or not

Tags:

c#

asp.net

Am I doing this correct? I'm interested to know the possible reasons why its failing?

Object obj = Find(id); //returns the object. if not found, returns null
if (!Object.ReferenceEquals(obj, null))
{
     //do stuff
}
else
{
     //do stuff
}

Find Method (Uses ORM Dapper). Performed unit tests on this, I believe there is no problem with this method.

public Object Find(string id)
{
     var result = this.db.QueryMultiple("GetDetails", new { Id = id }, commandType: CommandType.StoredProcedure);
     var obj = result.Read<Object>().SingleOrDefault();
     return obj;
}
like image 777
user Avatar asked Dec 01 '22 17:12

user


1 Answers

Try this:

   Object obj = Find(id); //returns the object. if not found, returns null
   if (obj != null)
   {
        //do stuff when obj is not null
   }
   else
   {
        //do stuff when obj is null
   }
like image 151
ekad Avatar answered Dec 04 '22 07:12

ekad