Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"..." cannot implement an interface member because it is not public

public interface IDatabaseContext : IDisposable {

    IDbSet<MyEntity1> Entities1 { get; set; }

}

public class MyDbContext : DbContext, IDatabaseContext {

    IDbSet<MyEntity1> Entities1 { get; set; }

}

Can't compile because of the error described in here: http://msdn.microsoft.com/en-Us/library/bb384253(v=vs.90).aspx

However, this makes no sence since the interface obviously IS public. What could be the error here?

like image 573
Acrotygma Avatar asked Feb 14 '14 15:02

Acrotygma


People also ask

Does not implement interface member because it is not public?

The error thrown is usually “Cannot implement an interface member because it is not public”. If an interface member method is implemented in your class without an access modifier, it is by default private. To change the visibility, you can either change the interface from public to internal.

CAN interface have public members?

An interface only has declarations of methods, properties, indexers, and events. An interface has only public members and it cannot include private, protected, or internal members.

Why interface members are public?

1) Interface members are only visible to code outside of the interface based on the rules of the respective visibility level. public : Interface members in C# are public by default, so this works.

Can we declare private method in interface C#?

Interface can contain declarations of method, properties, indexers, and events. Interface cannot include private, protected, or internal members. All the members are public by default. Interface cannot contain fields, and auto-implemented properties.


1 Answers

When implementing an interface member in the class, it should be public

See: Interfaces (C# Programming Guide)

To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.

public class MyDbContext : DbContext, IDatabaseContext {

    public IDbSet<MyEntity1> Entities1 { get; set; }
}

Or as @Marc Gravell said in comment you can do Explicit interface implemenation, More could be found at this answer

like image 66
Habib Avatar answered Sep 29 '22 00:09

Habib