Is there a performance penalty when returning an Interface?

Viewed 321

Consider an interface and a method that returns an object that implements this interface:

interface ILoggedData
{
    public int Id {get;}
    public string Description {get;}
}

class LoggedDataReader
{
    public ILoggedData GetLoggedData()
    {
        ...
        LoggedData loggedData = new LoggedData(...);
        return loggedData;
    }

    private class LoggedData : ILoggedData
    {
        ...
        public int Id {get; set;}
        public string Description {get; set;}
    }
}

So LoggedDataReader.GetLoggedData creates an object of private class LoggedData and returns the interface ILoggedData.

I assumed that the conversion from LoggedData to the interface doesn't do anything. You can regard it as a limitation for the compiler: "You can only use these getter properties". I assumed that just the reference to the loggedData object is returned, and interpreted as the reference to the interface.

Is this correct, or does the type conversion from object to interface actually do anything?

2 Answers

I assumed that the conversion from LoggedData to the interface doesn't do anything. You can regard it as a limitation for the compile

Well the problem is right there. If you think that using interfaces is useless and just hinders performance, then why are do interfaces exist to begin with?

The use or not of interfaces has very little to do with performance and all about implementation decoupling an polymorphism.

In 99.999999999999% of the situations the decision on using an interface or a concrete class should neverbe based upon performance reasons.

It very much depends on what type of interface you return. Let's look at three simple examples:

  1. Return class object as interface: Most likely neglible impact (as shown here)
public ILoggedData GetLoggedData() => new LoggedDataClass();
  1. Return struct object as interface: Boxing occurs, a common performance bottleneck (MSDN).
public ILoggedData GetLoggedData() => new LoggedDataStruct();
  1. Return list of objects as interface: Memory pressure increases, performance can suffer greatly on hot paths. Detailed explanation here.
public IEnumerable<ILoggedData> GetLoggedData()
{
    return new List<ILoggedData>() { new LoggedDataClass() };
}

The answer...

...is yes! In certain circumstances, the "type conversion" implies unwanted side-effects. The other answer does not take the inner workings of the C# compiler into consideration. E.g. this is incorrect:

The use or not of interfaces has very little to do with performance and all about implementation decoupling an polymorphism.

The use of interfaces makes it harder and sometimes impossible for the compiler to optimize certain operations. The most common pitfall is the GetEnumerator pattern used by the foreach keyword. With a strong type - e.g. List<T> - the compiler can use the optimized enumerator whereas the same is not true for a weak type - e.g. IList<T> or IEnumerable<T>.

Every C# developer should read Lippert's blog post about this.

Related