Alternative conditional operator

  • Thread starter Thread starter Robert Jacobson
  • Start date Start date
R

Robert Jacobson

Hi,

I'm trying to get a reference to an object in a collection. If the indexer
is outside of the range, it should return null, without raising an
IndexOutOfRange exception.

I'm currently using the conditional operator to do this, which is rather
kludgy:

TreeNode currentNode = i < nodes.Count ? nodes : null;


I vaguely recall another C# language construct that did the same thing
(return the object or null), but a bit more elegantly. Assuming I'm not
totally off-base, could anyone point me in the right direction?

Thanks,
Robert Jacobson
 
I think you are thinking of "as" for type conversion, which returns
null if the cast is invalid.
 
That's what I was thinking of (but not quite what I need.) Thanks much.


Norfy said:
I think you are thinking of "as" for type conversion, which returns
null if the cast is invalid.
 
Back
Top