Monkey()
and
Dog()
are not methods they are classes that implement the interface IAnimal.
Somewhere you will need something like
public class Monkey : IAnimal
{
public int getLegs()
{
return 2;
}
}
public class Dog : IAnimal
{
public int getLegs()
{
return 4;
}
}
See this CP article for more information
Interfaces in C# (For Beginners)[
^]
You are not inheriting from the Interface, you are implementing the interface.
Another CP article that might be useful
Abstract Class versus Interface[
^]
[EDIT]
OP asked
Quote:
Yes, I know that. I meant new IAnimal[] is kind of array? Can we do that?
Yes you can. Every item in the array must be an object of a class that implements IAnimal.
Note that when you use the elements in the array you may need to cast them to their original class (or a base class) before you can access all of their properties or methods (the one's that are unique to each class)
Also note that you can't use the (new) style of declaration
var animals = new[] { new Monkey(), new Dog() };
as you will get an error
Quote:
No best type found for implicitly-typed array
In other words, Visual Studio needs a little extra help to work out what the elements in the array will be :)