mo.notono.us

Thursday, July 29, 2004

Why doesn't this work?

public bool IsIn(object subject, params Type[] targetTypes)
{
	foreach (Type target in targetTypes)
	{
		if (subject is target)
			return true;
	}
	return false;
}

2 Comments:

  • If "subject is traget", you get a "true", but the loop does not stop.

    So you get a "false" right after that.

    This might work, because you get either "true" or "false", but never both.

    public bool IsIn(object subject, params Type[] targetTypes)
    {
    foreach (Type target in targetTypes) {
    if (subject is target)
    return true;
    else
    return false;
    }
    }

    By Anonymous Anonymous, at Tuesday, November 16, 2004 1:26:00 PM  

  • Sorry - no, that will simply exit the loop after the first item.

    I should have said - Why doesn't this compile?

    There's nothing wrong with the loop construct - the issue has to do with the compiler barfing on the "subject is target" line - the is construct apparently can't be used with an instance of a type.

    By Blogger Oskar Austegard, at Tuesday, November 16, 2004 4:45:00 PM  

Post a Comment

<< Home