Lets say we have an interface "ISomething" with the method "DoSomething".
And we create multiple classes that implement it. Example 5/10/100/1000 new class definitions.
will iterating a List < ISomething > and calling DoSomething() take the same amount of time, regardless of #unique classes added to the list. Assumption that list contains the same number of objects. Comparison list of different types vs list of same type.
Essentially I am asking if an interface method call is always a "Constant" runtime cost, or it depends on # unique types behind it.
Why am I asking this? I need to realise how interface works behinds the scenes to not trigger unexpected overhead.
Edit: Can I make the question a little harder ?
Lets consider the opposite scenario, we now have only 1 class but N interfaces it implements(ISomething1...N). Is the performance the same regardless on the #interfaces that it implements. Lets say that most of the interfaces end up pointing to the same method.
Edit2: adjan's answer raises another potential issue:
he said that c# generates one v-table per interface => all its methods cost O(1).
Lets consider the following scenario.
A vertical inheritance chain: A0 extends A1 extends A2 ... extends AN.
All these classes we construct them with no variables inside.
We just created a performance bug.
A0.method() costs O(N).
I0.method() costs O(1).
If interfaces can do the job with a constant overhead, why pay variable cost if someone abuses the inheritance mechanism and creates a vertical tree branch of depth N.
Does A0 virtual method cost O(N) ? What about its constructor, or its destructor, are we 100% safe to assume that it takes constant runtime cost. Regarding the constructor, a call to A0's constructor triggers A1's parameter-less constructor, thus the invocation chain continues... therefore O(N) cost.