C #: forced method naming method exists as part of an interface

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

+2
c # interface language-features annotations
Jan 29 '10 at 18:19
source share
9 answers

You have three options:

Explicitly implement the interface:

You can explicitly implement the interface using the syntax IInterface.MethodName , for example:

 bool IInterface.MethodName(string other) { // code } 

Wrap the widgets in the area

The tools already wrap your code in the region if you select "Embed Interface" through the interface:

 #region IInterface members public bool MethodName(string other) { // code } #endregion 

Document through comments

C # allows you to add several kinds of comments through // , /* or comments on XML documentation . Use liberally!

+4
Jan 29 '10 at 18:20
source share

Warm them with #region MyInterface Members , as Visual Studio does.

This reduces readability if you use #region only for interface members. If, on the other hand, you use it for private variables, properties, methods, events, and various interface members, the code will become much more readable. You can also group participants by functionality.

Another option is to use a tool that does this for you. For example, you can use NArrange-.NET Code Organizer / Formatter / Beautifier

+5
Jan 29 '10 at 18:22
source share

You may have deeper design issues if your class uses methods that you think should be removed.

In any case, Resharper can help you here.

http://www.jetbrains.com/resharper/documentation/presentation/overview/code_generation/Generate_demo.htm

Drag the slider to 10/69 to see the special interface icon.

Resharper tells you which interface is required for this method.

Edit: In addition, if you click on a method name, a text appears in it explaining where this method comes from. I'm not sure if this is part of Visual Studio or Resharper since I have been using Resharper for so long. Can anyone without Resharper confirm this?

+2
Jan 29 '10 at 18:30
source share

VB.NET has support for something similar, but not C #.

+1
Jan 29 '10 at 18:24
source share

Perhaps you want to expand your own custom attribute? - http://msdn.microsoft.com/en-us/library/sw480ze8(VS.80).aspx

You can create your own attributes by defining an attribute class, a class that derives directly or indirectly from an attribute that makes the identification attribute definitions in metadata quick and easy. Suppose you want to tag classes and structures with the name of the programmer who wrote the class or structure. You can define a custom Author attribute Class:

An example from the above description. You might want to apply this to methods and be something like @Override in Java.

 [Author("H. Ackerman", version = 1.1)] class SampleClass { // H. Ackerman code goes here... } 
+1
Jan 29 '10 at 18:36
source share

Can you use addin for Visual Studio? This will save you from marking the code in a way that cannot be verified, but has the disadvantage that you cannot get information when viewing the code outside of Visual Studio.

ReSharper adds an icon to the box to indicate this. Hover over it and it tells you what interface this method is with. Click it and it will lead you to the interface.

ReSharper http://img686.imageshack.us/img686/5325/34804250.jpg

I would say that CodeRush probably does something similar.

+1
Jan 29 '10 at 19:32
source share

You can explicitly implement an interface, but then this method will not be available in an instance of the class, unless you apply the class to this interface.

 public Foo : IDisposable { void IDisposable.Dispose() { } } 
0
Jan 29 '10 at 18:22
source share
 #region ISomeInterface implementation public void Foo(){} public void Bar(){} #endregion 
0
Jan 29 '10 at 18:27
source share

Another way, if your anti-region will use documentation comments as follows:

 ///<summary> /// Used for implementation of InterfaceX ///</summary> public void Foo() {} 
0
Jan 29 '10 at 18:32
source share



All Articles