开发者

Get Class level instance of a variable (not Interface)

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.NoseS开发者_如何学Goize}
        set { ClassLevel.NoseSize = value}
    }
}

How do I really indicate "ClassLevel"?


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.


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.


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;
    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜