views:

58

answers:

2

How to declare explicit a member of a interface?.i.e:

    public interface IPerfil
    {
        int IDPerfil
        {
            get;
            set;
        }
        int IDMarca
        {
            get;
            set;
        }
        int IDRegional
        {
            get;
            set;
        }
        int IDFilial
        {
            get;
            set;
        }
}

then

    public class ComentariosPerfil : BaseComentarios, IPerfil
    {
        public int IPerfil.IDFilial
        {
            get;
            set;
        }
[...]

I get an compilation error,saying that "public" modifier cannot be applied to this item.

The question is:

I want this property to be public. I can't write modifiers in interface like:

   public int IDPerfil
        {
            get;
            set;
        }

So,how can I explicitly implement an interface member, and make it Public?

+6  A: 

For explicitly implemented interfaces you can't specify the visibility. It is taken from the visibility in the interface's definition.

So in your case use the following. The function will be public because that's the way the IPerfil interface is defined:

public class ComentariosPerfil : BaseComentarios, IPerfil 
{ 
    int IPerfil.IDFilial 
    { 
        get; 
        set; 
    }
Russell McClure
You can't define visibility in an interface either, because an interface defines the publically available contract of a class, so all interface methods are public by default.
Femaref
@Femaref: Maybe you are confused by my use of the word "in". I'm talking about at the beginning of an interfaces definition, you can specify the visibility. Also, you seem to imply that you can only have public interfaces which is not true. You can easily create an internal interface and then explicity implement it.
Russell McClure
Yup, this seems to be the case.
Femaref
A: 

No, you can't. Explicitly implemeting an interface means you have to cast it to the interface type first to use the defined contract. All members of an interface are public by default, so a public explicit interface doesn't make any sense because you can't access it from the implementing class in the first place.

Femaref