0

I want to have a generic interface which has a property that is the used as the Id property of the derived classes.

I wrote the interface like below:

interface IEntity<T>
{
    T Id { get; set; }
}

and the derived classes can use it like below:

public class Employee : IEntity<int>
{
    // don't need to define Id property
    // public int Id { get; set; }
}

public class Document : IEntity<string> { ... }

Unfortunately, the compiler nags this error:

'Employee' does not implement interface member 'IEntity.Id'

What did i do wrong? thanks.

Edit: While the accepted answer solves the problem, the @dbc comments helped me to achieve my goal, if i changed the interface IEntity to abstract class IEntity it doesn't need to implement the Id property in the derived class.

Ehsan Toghian
  • 548
  • 1
  • 6
  • 26
  • 3
    You have to implement `int Id` in `Employee : IEntity`. Interface is a contract that assures others that `Employee` has `Id` property. You have to implement it. – FCin Jul 29 '18 at 07:45
  • Isn't there a method that doesn't need to implement that Id??? I created `IEntity` to reduce coding Id property in every entity class – Ehsan Toghian Jul 29 '18 at 07:47
  • 1
    In that case you'll need an abstract base class `Entity` which implements the interface by defining the Id property from the contract. The Employee class can derive from that base class – Ruud Kobes Jul 29 '18 at 07:48
  • 1
    You seem unsure of the difference between implementing an interface and deriving from a base class. You might check out [Difference Between Interface and Class](https://stackoverflow.com/q/15857982), [How to decide between an Interface or Base Class for an new implementation?](https://stackoverflow.com/q/15583896) or [What is the difference between an interface and abstract class?](https://stackoverflow.com/q/1913098). – dbc Jul 29 '18 at 07:50
  • Problem with abstract class will be, that you can inherit only from one class. – Fabio Jul 29 '18 at 07:58

3 Answers3

2

You are confusing inheritance and interface implementation.

When an interface inherits another interface, the members are inherited, and you do not repeat them:

interface IEntity<T>
{
    T Id { get; set; }     // necessary code for 'get' and 'set' not present (yet)
}
interface IEmployee : IEntity<int>
{
    // don't need to repeat Id property
    // it is inherited
}

Similarly when a class inherits another class:

class Entity<T>
{
    public T Id { get; set; }     // automatic code for 'get' and 'set' exists here
}
class Employee : Entity<int>
{
    // don't need to repeat Id property
    // it is inherited
}

The base class may be made abstract if you want to make sure only derived classes are instantiated.

But when a class (or a struct) implements and interface, every member of the interface must be implemented in some way. Usually by a public member of that class or struct. (That public member may be inherited from a base class!) Or, occasionally, by an explicit interface implementation.

An interface has no bodies, just signatures. For example the get and set accessors of the Id property must have some body to make sense. When you write T Id { get; set; } in an interface, there are no bodies of the accessors. But when you write T Id { get; set; } in a class (or struct), and there is no abstract or extern modifier, the semicolons have another meaning; then the compiler while auto-generate the necessary accessor bodies (and also auto-generate a field which is used by the accessors).

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
1

As in interface, ALL methods must be implemented!

Properties are nothing more than just methods, which have to be implemented when defined in interface: int Id { get; set; }.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • Yep, the properties are nothing but special methods. the interface properties just declare whether they are readonly or read-write. The concrete implementation must provide definition for it. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/interface-properties – kumarmo2 Jul 29 '18 at 08:06
0

You need to implement interface for every class - just like mentioned earlier:

interface IEntity<T>
{
    T Id { get; set; }
}

public class Employee : IEntity<int>
{
    public int Id { get; set; }
}
kasuocore
  • 99
  • 1