"As" cast
(_pages[(int)ePage.Setup] as usSetup).SetupMethod();
If _pages[(int)ePage.Setup]
is not usSetup
, you will get NullReferenceException
here, because (_pages[(int)ePage.Setup] as usSetup)
will return null.
Direct cast
((ucSetup)_pages[(int)ePage.Setup]).SetupMethod();
If _pages[(int)ePage.Setup]
is not usSetup
, you will get InvalidCastException
here.
Other than that, no differences.
By the way, I encourage you to follow .NET naming guidelines. Your classes should be named UCSetup
(or UcSetup
) and UCAddMorePagesHere
(or UcAddMorePagesHere
).
Alternative
You can simplify your code if you do the following:
public abstract class SetupPage : UserControl
{
public abstract void SetupMethod();
}
public class UcSetup : SetupPage
{
public override void SetupMethod()
{
// Do something here
}
}
public class UcAddMorePagesHere : SetupPage
{
public override void SetupMethod()
{
// Do something here
}
}
Then, instead of keeping list of UserControl
's, keep list of SetupPage
's
var _pages = new List<SetupPage>();
You will be able to call your method without a cast:
_pages[(int)ePage.Setup].SetupMethod();