Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hey I have an array of class:
C#
Empty = new EmptyPage[100];
for (int i = 0; i < 100; i++)
    Empty[i] = null;


If I do: if(Empty[0]==null) ==> always returns false;

What is the best way to check if an object is not defined or it is null?
Posted
Comments
Zoltán Zörgő 25-Dec-12 8:49am    
But it should work like this. What's the rest of the code?
missak boyajian 25-Dec-12 12:38pm    
Thanks. There was something wrong in the code.
Akinmade Bond 25-Dec-12 8:58am    
What is 'Empty'?
Oleksandr Kulchytskyi 25-Dec-12 9:02am    
I don't believe you!! if(Empty[0]==null) must work!!!
Oleksandr Kulchytskyi 25-Dec-12 9:03am    
Empty = new EmptyPage[100];??? Provide community with full list of code!!!

Your code looks ok.

To check for null you can always look at the null coalese operator[^].
 
Share this answer
 
apply If Else
C#
if(Empty[i].Equals(null))
{
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-Dec-12 15:40pm    
I cannot believe that someone else will be able to suggest such gibberish.
The answer is: someObject == null. I'm really sorry, Dheerendra, but the vote is 1...
By the way, in general case, ReferencesEqual should be used, as equality can be overridden.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900