Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation Error: "The modifier 'public' is not valid for this item" while explicitly implementing the interface

Tags:

c#

.net

oop

I am getting this error while creating a public method on a class for explicitly implementing the interface. I have a workaround: by removing the explicit implementation of PrintName method. But I am 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());

        }
    }
}
like image 776
Lalit Avatar asked Apr 19 '10 16:04

Lalit


3 Answers

When using explicit implementation of an interface, the members are forced to something more restricted than private in the class itself. And when the access modifier is forced, you may not add one.

Likewise, 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 (very) private? Consider:

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

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

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

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

For the same reason you cannot even call M() from inside a class C member. You will have to cast this to a specific interface first to avoid the same ambiguity.

class C : I1, I2
{
   ...
   void X() 
   {  
     M();             // error, which one? 
     ((I1)this).M();  // OK 
   }
}
like image 129
Henk Holterman Avatar answered Nov 09 '22 17:11

Henk Holterman


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

like image 16
hackerhasid Avatar answered Nov 09 '22 18:11

hackerhasid


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).

like image 10
Andrew Bezzub Avatar answered Nov 09 '22 18:11

Andrew Bezzub