Skip to content

Interface declaration

Rajakulai Setty

Asked May 11, 2015, 11:23 PM EDT1 answer
Share
LinkedInX

Hi All,

I need some general clarification regarding Interface template, like below

Code
 public interface ISomeInterface
Code
{
Code
    string Id { get; set; }
Code
    string Name { get; set; }
Code
    ISomeInterface WorkerInstance();
Code
}

In the above code they used method return type as interface in same interface declaration. Am not sure why they coded like this. Please share your comments

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Pranay Rana
    0
    its basically says the return type is always going to be of sub type of interface type and it provide flexibility. basically it abstract return type and you dont want to expose your actual implementation to the user of your class (Specially when you expose your code as libarary). this helpful because you can extend your inteface easily than class concrete implementation.


    example

    interface IA
    {
      IA methodA();
    }


    public class A : IA
    {
      public IA methodA()
      {
      var A = new A();
      return A;
      }
    }

    Best answer for this : http://stackoverflow.com/questions/400135/c-sharp-listt-or-ilistt
    0