tags:

views:

301

answers:

3

I am getting this error while creating public method on a class for explicitly implementing the interface. I have the workaround: by removing the explicit implementation of PrintName Method, But surprised why i am getting this error.

Can anyone explain the error.

Code for Library: using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Test.Lib1
{

    public class Customer : i1 
    {
        public string i1.PrintName() //Error Here...
        {
            return this.GetType().Name + " called from interface i1";
        }
    }

    public interface i1
    {
        string PrintName();
    }

    interface i2
    {
        string PrintName();
    }
}

Code for Console Test Application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Test.Lib1;

namespace ca1.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            Console.WriteLine(customer.PrintName());

            //i1 i1o = new Customer();
            //Console.WriteLine(i1o.printname());

            //i2 i2o = new Customer();
            //Console.WriteLine(i2o.printname());

        }
    }
}
+2  A: 

You cannot use access modifiers when implementing interface explicitly. Member will be binded to the interface anyway, so it is no need to specify access modifier, because all interface members are always public, also all explicitly implemented members can be accessed only through member of interface type (see statichippo's answer for example).

Andrew Bezzub
"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." Relevant documentation (http://msdn.microsoft.com/en-us/library/aa664591%28v=VS.71%29.aspx).
R0MANARMY
+3  A: 

http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx : When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface.

Customer customer = new Customer();

Console.WriteLine(customer.PrintName());

Violates this

statichippo
+2  A: 

When using explicit implementation of an interface, the members are (forced to) private in the class itself. And when the access modifier is forced, you may not add a modifier.

Similar, in the interface itself, all members are public. If you try to add a modifier inside an interface you will get a similar error.

Why are explicit members private? Consider:

interface I1 { void M(); }
interface I2 { void M(); }

class C : I1, I2
{
    void I1.M() { ... }
    void I2.M() { ... }
}

C c = ...;
c.M();  // Error, otherwise: which one?
(c as I1).M(); // no problem

If those methods were public, you would have a name-clash that cannot be resolved by the normal overload rules.

Henk Holterman
(c as I1).M(); // no problem It is correct, but what if you want to access the method M outside the assambly, It is not accessible?
Lalit
@Lalit, it is accessible everywhere I1 is accessible. Also in other assemblies.
Henk Holterman
Interface I1 is accessible outside assambly but Method M for Class C is not accessible ouuside, Check the class browser as well it is marked as private.
Lalit
If I1 is accessible then it should work. If not, ask a separate question.
Henk Holterman