C#: enforceable way of signifying a method is there as part of interface
Is there a way in C# to mark a method as being part of a class to satisfy an interface that the class implements? I find myself wondering sometimes when digging in a class's code why some methods are there but then, when I try to remove one since it isn't in use, I see it's necessary in order for the class to implement some interface. It'd be nice to have these methods marked as such. Something like the @Override
annotation in Java, maybe.
Edit: I would prefer to not explicitly implement the interface because then accessing the interface methods on an instance of my class becomes more of a hassle (i.e., having to cast an instance of MyClass
to be IMyInterface
before calling MyInterfaceMethod
).
Edit: I would also prefer not to use regions. I don't want a big block of code described loosely through some arbitrary name as being part of an interface implementation开发者_如何学编程, but rather to denote specific methods, wherever they may be within the class, as being part of an interface. Even some XML comment template that's intended for saying which interface the method belongs to would be nice.
Edit: All the answers seem to suggest I explicitly implement interfaces, which I don't want to do, or that I use regions, which I also don't want to do. I think Will understood my request best. I was hoping for something like an annotation, so I could do something like the following:
[Interface(IMyInterface)]
public void MyImplicitlyImplementedInterfaceMethod() { }
Or, as dss539 mentioned in a comment to this answer:
public implement void MyImplicitlyImplementedInterfaceMethod() { }
Much like we have override
today: public override bool Equals(object obj) { }
.
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 interface members in a region
Tools will already wrap your code in a region if you choose to "Implement an interface" through the UI:
#region IInterface members
public bool MethodName(string other)
{
// code
}
#endregion
Document through comments
C# allows you to add multiple kinds of comments through //
, /*
, or XML documentation comments. Use liberally!
Surround them with #region MyInterface Members
as Visual Studio does for you.
It 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 different interface members the code will become much more readable. You can also group members by functionality.
One more option is to use a tool that does it for you. For example you can use NArrange - .NET Code Organizer/Formatter/Beautifier
Maybe you have deeper design issues if your class is implementing methods that you feel should be deleted.
Anyways, Resharper can help you out here.
http://www.jetbrains.com/resharper/documentation/presentation/overview/code_generation/Generate_demo.htm
Drag the slider to 10/69 to see the special "Implements interface" icon.
Resharper tells you which interface requires the method.
Edit: Also, if you mouse over the method name, it pops up text explaining where the method comes from. I'm not sure if this is part of Visual Studio or Resharper since I've used Resharper so long. Can anyone without Resharper installed confirm this?
VB.NET has support for something like this, but not C#.
Maybe you want to roll out your own Custom Attribute? - http://msdn.microsoft.com/en-us/library/sw480ze8(VS.80).aspx
You can create your own custom attributes by defining an attribute class, a class that derives directly or indirectly from Attribute, which makes identifying attribute definitions in metadata fast and easy. Suppose you want to tag classes and structs with the name of the programmer who wrote the class or struct. You might define a custom Author attribute class:
The example from above description. You may want this to apply to methods, and to be something similar to @Override in Java.
[Author("H. Ackerman", version = 1.1)]
class SampleClass
{
// H. Ackerman's code goes here...
}
Can you make use of an addin for Visual Studio? This will save you from marking up your code in a way that cannot be validated, but does have the downside that you will not be able to get the information when viewing the code outside of Visual Studio.
ReSharper adds an icon in the margin to signify this. Hover over it and it tells you which interface the method is from. Click it and it takes you to the interface.
ReSharper http://img686.imageshack.us/img686/5325/34804250.jpg
I'd say that CodeRush probably does something similar.
You can explicitly implement the interface, but then the method won't be available on an instance of the class unless you first cast the class to that interface.
public Foo : IDisposable
{
void IDisposable.Dispose()
{
}
}
#region ISomeInterface implementation
public void Foo(){}
public void Bar(){}
#endregion
Another way if your anti region would be to use the documentation comments like so:
///<summary>
/// Used for implementation of InterfaceX
///</summary>
public void Foo() {}
精彩评论