Search code examples
c#wcf-ria-services

How does the "is" keyword work?


More specifically, why does this work:

foreach (ChangeSetEntry changeRow in changeSet.ChangeSetEntries)
    if (changeRow is RouteStage)
    { ... }

but this not?

ChangeSetEntry changeRow = changeSet.ChangeSetEntries[0];
if (changeRow is RouteStage)
{ ... }

In the latter case, I get a compiler warning saying:

The given expression is never of the provided type.

I can understand that, as changeRow is a ChangeSetEntry not a RouteStage, so why does it work inside the foreach block?

This is in my override of the Submit method in an RIA Services DomainService. RouteStage is an entity I have defined to be returned by the DomainService.


Solution

  • The line should read:

    if (changeRow.Entity is RouteStage)
    

    ...then it compiles cleanly. One of those cases of "can't see for looking". TFS highlighted what I had inadvertently changed.