C#: enforceable way of signifying a method is there as part of interface

纵饮孤独 提交于 2019-12-06 08:15:08

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() {}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!