Compiler says I am not implementing my interface, but I am?

Interface implementations need to be public or explicit: Public: class Account : IAccount { public string SomeMethod() { return "Test"; } } Explicit: class Account : IAccount { string IAccount.SomeMethod() { return "Test"; } } The default in C# is private if you do not specify the access modifier.

What's interesting of course is that you can't specify any access modifier for explicitly implemented interfaces. I would be curious to see the IL generated for these. I mean, it can be called by an outside class, but only if you cast it to that interface first.It's actually pretty weird.

:) – aquinas Jun 14 at 20:12 That's actually really useful. I have interfaces that divide up layers of my application and I would never want them to be used without their interfaces. Explicitly implementing the interface is exactly what I need to do :) – Alex Ford Jun 14 at 20:24.

Methods implementing interface needs to be public. In your later case, you are declaring it explicitly. This is what specification says about explicit interface implementation.

Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through their fully qualified name in a method invocation or a property access, they are in a sense private. However, since they can be accessed through an interface instance, they are in a sense also public.

Thank you very much. – Alex Ford Jun 14 at 20:04.

Project.DataAccess.Concrete.Account. SomeMethod' cannot implement an interface member because it is not public namespace Project.DataAccess. Concrete { public class Account : IAccount { public string IAccount.SomeMethod() { return "Test"; } } }.

As I SPECIFICALLY stated. Making it public fixes it, but why does specifying IAccount. SomeMethod and NOT making public fix it as well?

– Alex Ford Jun 14 at 20:00 Presumably since interface members MUST be public so when it's explicitly declared, the compiler is smart enough to know to make it public, but if you don't explicitly bind it to IAccount, the compiler isn't sure you don't mean some OTHER "SomeMethod()" – The Evil Greebo Jun 14 at 20:02.

You're not mentioning in your class declaration that you'll be implementing IAccount. Your class declaration should look like this: class Account : IAccount { //Implementation here. } Also, what's happening is that you're using the "default" protection level for Account, and that protection level isn't "public", but an Interface (IAccount) defines public methods by default.So, when you preface both the Class and Method names with public you're actually implementing the interface.

Likewise, when you declare SomeMethod as IAccount.SomeMethod() { //Implementation Here } what you're doing is Explicitly Implementing the interface.

You have 2 basic options when implementing intefaces: implicit or explicit. Implicit implementation takes on the form of a public method or property, while explicit is in the form of a method or property prefaced with the IFoo. Modifier that is otherwise not public.

Interface IFoo { void Bar(); } class FooA : IFoo { public void Bar() { } } class FooB : IFoo { void IFoo.Bar() { } } The difference here is that in the case of FooA, void Bar is part of the publicly visible API of the class. Code can call Bar via the instance of the class. FooA foo = new FooA(); foo.Bar(); // legal In the case of FooB, void Bar is not part of the publicly visible API of the class.

The method can still be called, but it must explicitly be called via the interface. FooB foo = new FooB(); foo.Bar(); // not legal IFoo myFoo = foo; myFoo.Bar(); // legal Your code does not compile because it walks in the uncharted waters between an implicit and explicit interface implementation. You saw that with your change, you had legally defined an explicit implementation, which is why that particular version compiles.

Otherwise, you've also found that the public modifer is needed to make the non-explicit version compile.

I think declaring it explicitly with string IAccount.SomeMethod() works because . NET knows that this implementation of the method can only be accessed through the interface, and so it carries over the public visibility from the interface. In other words, since you are explicitly saying it is an interface member, the compiler can imply that it has to be public, so you don't have to re-state the obvious.

This is also discussed here: msdn.microsoft.com/en-us/library/aa66459... It is a compile-time error for an explicit interface member implementation to include access modifiers, and it is a compile-time error to include the modifiers abstract, virtual, override, or static. Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through their fully qualified name in a method invocation or a property access, they are in a sense private.

However, since they can be accessed through an interface instance, they are in a sense also public.

Class Account : IAccount { public string SomeMethod() { return "Test"; } } Tell your class to implement the interface. Using the Interface. Method name explicitly implements the method (but i'm not sure why or how) and you need to make them public.

You want to use Interface. Method to explicitly implement an interface requirement when the class already has a member of the same name class MyClass : IAccount { public int SomeMethod() {} public string IAccount.SomeMethod() {} }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions