Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hey I have an array of class:
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 25-Dec-12 3:35am
Comments
Zoltán Zörgő at 25-Dec-12 8:49am
   
But it should work like this. What's the rest of the code?
missak boyajian at 25-Dec-12 12:38pm
   
Thanks. There was something wrong in the code.
GeekBond at 25-Dec-12 8:58am
   
What is 'Empty'?
Oleksandr Kulchytskyi at 25-Dec-12 9:02am
   
I don't believe you!! if(Empty[0]==null) must work!!!
Oleksandr Kulchytskyi at 25-Dec-12 9:03am
   
Empty = new EmptyPage[100];??? Provide community with full list of code!!!
rizwan muhammed khan gouri at 22-Jan-13 8:51am
   
Hello paste Your Complete code ??????????/
missak boyajian at 22-Jan-13 14:54pm
   
Hey it was working. The problem was elsewhere.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Your code looks ok.
 
To check for null you can always look at the null coalese operator[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

apply If Else
if(Empty[i].Equals(null))
{
}
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100