I have this code. It wasn’t obvious at the time, but the code as written is always going to pick the first option since both “fc” and “fcip” start with “fc”.
string fcportdelimit = "fc";
string fcipportdelimit = "fcip";
if (BlockToProcess[0].StartsWith(fcportdelimit))
{
try
{
this.ParseFCInterface(BlockToProcess);
}
catch (Exception E)
{
throw;
}
}
else if (BlockToProcess[0].StartsWith(fcipportdelimit))
{
try
{
this.ParseFCIPInterface(BlockToProcess);
}
catch (Exception E)
{
throw;
}
}
I looked through the string class but don’t see a StartsWith() or Contains() that takes a pattern as input. The string I am testing against is either going to be a patttern fcN/N or fcipN where N is a number. So, I am thinking I would have to do something like this?
if (BlockToProcess[0].StartsWith(fcportdelimit || fcipportdelimit)
{
if (BlockToProcess[0].StartsWith(fcipportdelimit)
{
// do something here
}
else
{
//since fcipportdelimit didn't match it must be an fcport
//so do something else
}
}
Given that
StartsWith("fcip")impliesStartsWith("fc"), just test for the latter first and nest the second test.Furthermore, your
tryblocks are completely redundant and serve no function.(Of course, the second check still contains a redundant part since it checks for
fcagain but refactoring this check just makes the code less readable and won’t necessarily benefit performance.)