B
Bobby C. Jones
Is there an advantage to either of these methods, or is it simply a matter
of style? I used to use the "as" method, but later gave it up for the "is"
method as the intent of the code seems a little clearer to me. I now have
some projects coming up where the baseTypeCollection will contain 5k to 10k
objects that will have to be searched for various sub types of which I only
expect to find ~50 to ~100 matches. Thanks!
foreach (baseType in baseTypeCollection)
{
if (baseType is desiredSubType)
{
typeFound = (desiredSubType) baseType;
//do my thing with typeFound
}
}
foreach (baseType in baseTypeCollection)
{
typeFound = baseType as desiredSubType;
if (typeFound != null)
{
//do my thing with typeFound
}
}
of style? I used to use the "as" method, but later gave it up for the "is"
method as the intent of the code seems a little clearer to me. I now have
some projects coming up where the baseTypeCollection will contain 5k to 10k
objects that will have to be searched for various sub types of which I only
expect to find ~50 to ~100 matches. Thanks!
foreach (baseType in baseTypeCollection)
{
if (baseType is desiredSubType)
{
typeFound = (desiredSubType) baseType;
//do my thing with typeFound
}
}
foreach (baseType in baseTypeCollection)
{
typeFound = baseType as desiredSubType;
if (typeFound != null)
{
//do my thing with typeFound
}
}