IMytask
is the interface
which has some method, Mytask
inherits IMytask
why below one is always recommended way to declare a variable?
IMytask xyz = new Mytask();
Can you please help me to understand, What is the use and importance of doing this ?
3
Answers
I wouldn’t say this is always the recommended way to declare a variable involving a type implementing an interface but it often makes sense to do s. An interface represents a contract and by declaring a variable of that contract, you decouple the value of the variable of the implementation.
Consider this interface:
A variable of type IHelloWorld could now represent either type Foo or type Bar’s implementation.
It isn’t necessarily "important" – it is entirely possible for interfaces to be unnecessary and superfluous – and it is possible for them to be essential. That is contextual. I certainly wouldn’t say that it is a default / automatically recommended approach; I would have just used:
or
Either of which would leave
xyz
types asMytask
, notIMytask
.Interfaces separate API from implementation. This allows types to implement a particular API without having to share any kind of type model, or allows for the consumer to use an API without knowing which actual concrete types will be used – just: what API they provide.
For example, "sort" functions in the .NET framework code (
List<T>
etc) might use theIComparable<T>
orIComparer<T>
APIs, but the types that provide the compare functionality are often in your code. The framework can’t know about your code, just the interface.Likewise, interfaces may make it easier to inject, mock, or test code.
Or: it might add nothing useful! If there is exactly one type that implements an interface, and the consuming code could just have easily have used the type directly: the interface is probably overkill.
There are also some side aspects in terms of "explicit interface implementation" (which allows a type to implement methods of an API without having them on the public/default API of the type). Again, whether this matters: contextual.
Generally speaking, you would NOT declare a local variable as an interface; instead, you would just declare it as the concrete type or use
var
(which has the same effect as specifying the concrete interface in this case). This can avoid certain performance hits, as mentioned in the other answers.However, it’s important to know that in some cases you will see a functional difference between declaring a variable using an interface and using
var
or the concrete type. This can happen if the concrete type usesexplicit interface implementation
to implement an interface.An example will illustrate. Consider the following simple interface:
Now consider this implementation:
With that, look at the following usage:
So if you call
SayHello()
via the concrete class, you get a DIFFERENT result from callingSayHello()
via the interface.In cases like this, it depends on the context whether you need to use the concrete class or the interface (and I’d say that ending up with this dilemma is the result of a bad design).