Is there a way in C # to mark a method as part of a class to satisfy the interface that the class implements? Sometimes I sometimes wonder when I dig up class code, why there are some methods, but then when I try to remove it, since it is not used, I see that it is necessary for the class to implement some interface. It would be nice if these methods were marked as such. Perhaps something like @Override annotations in Java.
Edit: I would prefer not to explicitly implement the interface, because then access to the interface methods in the instance of my class becomes more troublesome (i.e., the MyClass instance must be IMyInterface before calling MyInterfaceMethod ).
Edit: I would also prefer not to use regions. I do not want a large block of code to be described freely through some arbitrary name as part of the implementation of the interface, but rather designate specific methods, wherever they are inside the class, as part of the interface. Even some XML comment template designed to indicate the interface to which this method belongs will be nice.
Edit: all answers seem to suggest that I explicitly implement interfaces that I don't want to do, or that I use regions that I also don't want to do. I think Will understood my request best. I was hoping for something like annotation, so I could do something like the following:
[Interface(IMyInterface)] public void MyImplicitlyImplementedInterfaceMethod() { }
Or, as dss539 is mentioned in a comment on this answer :
public implement void MyImplicitlyImplementedInterfaceMethod() { }
We really like override today: public override bool Equals(object obj) { } .
c # interface language-features annotations
Sarah Vessels Jan 29 '10 at 18:19 2010-01-29 18:19
source share