skip to Main Content

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


  1. 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:

    interface IHelloWorld
    {
       void Hello();
    }
    
    // You could have two implementations of the interface
    
    class Foo : IHelloWorld
    {
       public void Hello()
       {
          Console.WriteLine("Hello Earth!");
       }
    }
    
    class Bar : IHelloWorld
    {
       public void Hello()
       {
          Console.WriteLine("Hello Mars!");
       }
    }
    

    A variable of type IHelloWorld could now represent either type Foo or type Bar’s implementation.

    IHelloWorld impl = new Foo();
    
    impl = new Bar();
    
    impl.Hello();
    
    Login or Signup to reply.
  2. 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:

    var xyz = new Mytask();
    

    or

    Mytask xyz = new();
    

    Either of which would leave xyz types as Mytask, not IMytask.

    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 the IComparable<T> or IComparer<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.

    Login or Signup to reply.
  3. 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 uses explicit interface implementation to implement an interface.

    An example will illustrate. Consider the following simple interface:

    public interface IDemo
    {
        void SayHello();
    }
    

    Now consider this implementation:

    public sealed class Demo: IDemo
    {
        public void SayHello()
        {
            Console.WriteLine("Wave Goodbye");
        }
    
        void IDemo.SayHello() // Explicit implementation of "SayHello()".
        {
            Console.WriteLine("Hello!");
        }
    }
    

    With that, look at the following usage:

    Demo demo = new Demo();
    demo.SayHello(); // Outputs "Wave Goodbye"
    
    IDemo idemo = demo;
    idemo.SayHello(); // Outputs "Hello!"
    

    So if you call SayHello() via the concrete class, you get a DIFFERENT result from calling SayHello() 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).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search