samedi 29 novembre 2014

Class inherits from interface with extra property causes error


Vote count:

0




I understand that interfaces don't allow fields. But why can't the class that implements the interface have a property/field. I have tried researching the cause of this, but to no avail. Could someone point me to a resource that can help me.


Can classes only implement the method they inherit from the interface and nothing else?


Here is my code:



using System;

interface IDog
{
void Bark();
}

class Dog : IDog
{
public int numberOfLegs = 24;
public void Bark()
{
Console.WriteLine("Woof!");
}
}

class Program
{
static void Main()
{
IDog Fido = new Dog();
Fido.Bark();
Console.WriteLine("Fido has {0} legs", Fido.numberOfLegs);
}
}


I get the error:


'IDog' does not contain a definition for 'numberOfLegs' and no extension method 'numberOfLegs' accepting a first argument of type 'IDog' could be found (are you missing a using directive or an assembly reference?)



asked 45 secs ago







Class inherits from interface with extra property causes error

Aucun commentaire:

Enregistrer un commentaire