tags:

views:

51

answers:

4

If I have the following scenario

public interface IFace
{
     int NoseSize {get; set;}
}


public class Face: IFace
{
    private int NoseSize;

    public int IFace.NoseSize
    {
        get { return ClassLevel.NoseSize}
        set { ClassLevel.NoseSize = value}
    }
}

How do I really indicate "ClassLevel"?

+7  A: 

Simply refer to it as NoseSize or this.NoseSize if you like.


By the way, you are not correctly implementing the interface. If you want to use explicit interface implementation, you cannot specify an access modifier.

If you want to implement the interface implicitly, you should omit the interface name and simply have a public member that matches the interface member name and signature. Of course, if you do that, you can't have an identically named field in the same class.

Mehrdad Afshari
+3  A: 

You would alleviate the name clash by renaming:

private int NoseSize;

to:

private int noseSize;

I'm sure it varies by region, company, etc, but I believe it's typical to use camelCase for private members.

JMD
Actually, it's `camelCase` that's recommended for `private` fields. `PascalCase` is this one.
Mehrdad Afshari
and some people prefer using an underscore prefix when naming private fields such as private int _noSize;
Beatles1692
Thank-you, Mehrdad. I have some sort of long-standing mental block ensuring that I will *always* mix up camelCase and PascalCase. :facepalm:
JMD
+1  A: 

think of an interface as a Contract. What you are doing is saying that anything that is derived from your interface (IFace) is gaurenteed to have these members and/or methods. It is up to the deriving class to actually implement them. The other thing that it does for you is provides "IsA" relationship for the classes that are derived from the interface.

alternatly, you could do like this....

public class Face: IFace
{
    public int NoseSize
    {
        get;
        set;
    }

    public void foo()
    {
        this.NoseSize = 42;
        int someSize = this.NoseSize;
    }
}
Muad'Dib
+1  A: 

You should use the InterfaceName.Member notation in order to refer to a member that is implemented using explicit interface implementation.So if you omit the IInterfaceName. part it means you are referring to a non explicit implemented member (class level member as you call it).

Beatles1692