Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best and fastest way to check if an object is null [duplicate]

Tags:

c#

.net

I often see in source codes the usage of if (object.ReferenceEquals(myObject, null)) for checking if myObject is null instead of if (myObject == null) which I am familiar with.

Is there any particular reason (like speed, readability, etc) for using the first way instead of the second one? Which one do you use?

Thank you in advance.

like image 916
Dummy01 Avatar asked Dec 10 '10 10:12

Dummy01


People also ask

How do you check if an object is null?

Typically, you'll check for null using the triple equality operator ( === or !== ), also known as the strict equality operator, to be sure that the value in question is definitely not null: object !== null . That code checks that the variable object does not have the value null .

How do you check if something is null in C++?

In C or C++, there is no special method for comparing NULL values. We can use if statements to check whether a variable is null or not.

How do you check an object is null or not in Java?

In order to check whether a Java object is Null or not, we can either use the isNull() method of the Objects class or comparison operator.

How do you use an if statement with null?

Use the ISNULL function with the IF statement when you want to test whether the value of a variable is the null value. This is the only way to test for the null value since null cannot be equal to any value, including itself. The syntax is: IF ISNULL ( expression ) ...


1 Answers

Simple things are usually the most efficient : (myObject == null) is more performant

Look at this article

like image 179
Mehdi LAMRANI Avatar answered Oct 21 '22 22:10

Mehdi LAMRANI