+3  A: 
foreach(UserControl uc in plhMediaBuys.Controls)
{
  if (uc is MySpecificType)
  {
    return (uc as MySpecificType).PulblicPropertyIWantAccessTo;
  }
}
Kon
Actually, if you use the "as" keyword or just cast the variable, you are doing the same thing. The only difference between the two is "as" returns null if it can't cast, instead of throwing an exception.
Chris Pietschmann
True, but it'll never hit that line if it's not able to cast.
Kon
+7  A: 
foreach(UserControl uc in plhMediaBuys.Controls) {
    MyControl c = uc as MyControl;
    if (c != null) {
        c.PublicPropertyIWantAccessTo;
    }
}
Chris Pietschmann
I prefer this way of doing it although fallen888's will work too.
Marcus King
In fact, this example seems less efficient to me, because you're creating another instance of MyControl.
Kon
This code doesn't actually create a new instance of MyControl, it just creates a new reference to one. References are essentially pointers, so there should be no performance penalty here.
Charlie
Actually, if you use the "as" keyword or just cast the variable, you are doing the same thing. The only difference between the two is "as" returns null if it can't cast, instead of throwing an exception.
Chris Pietschmann
I stand corrected, however it still seems unnecessary - adds onto the complexity of the code.
Kon
do I need some sort of using directive or an assembly reference in my code behind that refrences MyControl?
rockstardev
Perhaps it is more complex to someone unfamiliar with C#, but it also saves a cast (quasi-expensive).
SoloBold
Ouch, that's hurtful, SoloBold. :) I just meant it adds to code congestion.
Kon
figured it out, thanks!
rockstardev
I didn't mean you wouldn't be able to understand it fallen :)
SoloBold
+1  A: 

Casting

I prefer to use:

foreach(UserControl uc in plhMediaBuys.Controls)
{
    ParticularUCType myControl = uc as ParticularUCType;
    if (myControl != null)
    {
        // do stuff with myControl.PulblicPropertyIWantAccessTo;
    }
}

Mainly because using the is keyword causes two (quasi-expensive) casts:

if( uc is ParticularUCType ) // one cast to test if it is the type
{
    ParticularUCType myControl = (ParticularUCType)uc; // second cast
    ParticularUCType myControl = uc as ParticularUCType; // same deal this way
    // do stuff with myControl.PulblicPropertyIWantAccessTo;
}

References

SoloBold