Using "as" and expecting a null return
Posted
by DrLazer
on Stack Overflow
See other posts from Stack Overflow
or by DrLazer
Published on 2010-05-20T11:15:52Z
Indexed on
2010/05/20
11:20 UTC
Read the original article
Hit count: 149
c#
For example. Lets say we have a stackpanel on a form. Its full of both Grids and Labels. I want to loop through all the Grids and do some operation on them but leave the Lables intact. At the moment I am doing it this way.
foreach(UIElement element in m_stacker.Children)
{
Grid block = element as Grid;
if(block != null)
{
//apply changes here
}
}
So i'm using the fact that "as" returns null if it cannot cast into the required type. Is this an ok thing to do or is there a better solution to this problem?
© Stack Overflow or respective owner