Question: I'm running into a problem with overload resolution in C# in a specific case. In my Razor file, I have the following:

```
@foreach (var result in Model.Result)
{
    @SearchResult(result)
}

@helper SearchResult(IEntity entity)
{
    <p>A normal thing</p>
}

@helper SearchResult(IPhoto photo)
{
    <p>A photo! Its title is @photo.Title</p>
}

```

Class structure:

```
interface IPhoto : IContentItem
interface IContentItem : IEntity

class Photo : ContentItem, IPhoto
class ContentItem : Entity, IContentItem
class Entity, IEntity

```

The actual instances being passed are of Photo.

`SearchResult(IEntity)` gets called for every instance when `SearchResult(IPhoto)` should be called (or the most specific overload of the instance of whatever the IEntity derivative is). How can I do what I'm trying to do without having to resort to this?

```
if (result is IXXX) { SearchResultXXX((IXXX)result) }
else if (result is IYYY) { SearchResultYYY((IYYY)result) }
...

```

Answer: 
What is the type of Property `Model.Result`? My guess is that it's `IEntity`.

**The decision which overload will be called is done during compile time not run time**, therefore it does not matter what the type of the instance is, it will always call the `SearchResult(IEntity entity)` method.

**UPDATE**

This is one possible solution to this problem:

```
@foreach (var result in Model.Result)
{
    @if(result is IPhoto)
    {
       @SearchResult(result as IPhoto)
    } 
    else 
    {
       @SearchResult(result)
    }
}

```
You could try using a double dispatch (ie: Visitor) pattern to get you a little closer. But you will still have to check to see if it is something that is not an IEntity (unless you have control over the IEntity interface).

```
interface IContentItem {
  void Accept(IContentVisitor visitor);
}

class Photo : IPhoto {
  void Accept(IContentVisitor visitor) { visitor.Visit(this); }
}

interface IContentVisitor<T>{
  T Visit(IPhoto photo);
  T Visit(IEntity entity);
}

class ContentVisitor : IContentVisitor<string>{
  string Visit(IPhoto photo) { return "<p>A normal thing</p>"; }
  string Visit(IEntity entity) { return "<p>A normal thing</p>"; }
}

var visitor = new ContentVisitor();
@foreach (var result in Model.Result)
{

    if(result is IContentItem)
       result.Accept(visitor);
    else //assuming result is IEntity
       visitor.Visit(result);
}

```