Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a method returns an interface, what does it mean?

I see many methods that specify an interface as return value. Is my thought true that it means: my method can return every class type that inherits from that interface? if not please give me a good answer.

like image 480
masoud ramezani Avatar asked Mar 04 '10 14:03

masoud ramezani


1 Answers

Yes, your method could return any type that implements that interface.

Here is an example:

using System;

class Foo
{
    public IComparable GetComparable()
    {
        // Either of these return statements
        // would be valid since both System.Int32
        return 4;
        // and System.String
        return "4";
        // implement System.IComparable
    }
}
like image 167
Andrew Hare Avatar answered Nov 14 '22 20:11

Andrew Hare