I have three classes that inherit from a class because I want those objects to be treated as the base class:
public class Find
{
}
public class StringFind : Find
public class DateFind : Find
public class LongFind : Find
ok then I create an array of ‘Find’ objects and I want to test if each object is of type StringFind , DateFind or LongFind so I created something as:
protected bool CompareUnion(params Find[] f)
{
foreach (var searchL in f)
{
if (f is StringFind)
{
//do something
var a = 3;
}
}
return true;
}
why f cannot be of type StringFind if StringFind inherts from that class? I thought that visual studio was wrong but I never hit the breakpoint and I am actually creating an array of StringFind objects.
It’s just a typo. You need:
fis just the array ofFindobjects you passed in, so it will only satisfyf is Find[].Usually when I see code like that, though, I start thinking of ways to avoid it. Rather than testing for a specific type and changing the behaviour, why not have a
virtualmethod and tailor the behaviour by overriding the method in the derived classes?