Say I have this constructor:
/// <summary> /// Example comment. /// </summary> public SftpConnection(string host, string username, string password, int port) {...}
which has these overloads:
public SftpConnection(string host, string username, string password) : this(host, username, password, 22) { } public SftpConnection(string host, string username, int port) : this(host, username, "", port) { } public SftpConnection(string host, string username) : this(host, username, "", 22) { }
and in reality, the XML comment is pretty large, with param
, example
and exception
elements and so on.
Is there some way to add a special XML comment one-liner to the overloads, such that they use the exact same comments so that I don't need to copy-paste the whole, enormous original comments?
I'm thinking something like: <use cref="SftpConnection(string,string,string,int)" />
which doesn't work of course.
I am aware of the include
element, but I get the impression it reads the comments from an XML file instead, which I don't want - I want the comment to still be visible in the code, but only once.
Thanks :-)
The syntax for adding XML comments in your code is triple slashes /// followed by one of the supported XML tags. There's IntelliSense support for writing documentation comments that also provides a template comment on entering the third slash in the triple slash.
Overloaded methods are differentiated based on the number and type of parameter passed as arguments to the methods. If we try to define more than one method with the same name and the same number of arguments then the compiler will throw an error.
The compiler distinguishes overloaded methods by their signatures—a combination of the method's name and the number, types and order of its parameters, but not its return type. If the compiler looked only at method names during compilation, the code in Fig.
In C#, there might be two or more methods in a class with the same name but different numbers, types, and order of parameters, it is called method overloading.
You can’t really do this. I find it annoying too.
However, you can alleviate the problem by using default parameter values instead of lots of overloads. Instead of:
public SftpConnection(string host, string username, string password, int port) public SftpConnection(string host, string username, string password) public SftpConnection(string host, string username, int port) public SftpConnection(string host, string username)
You can have just a single one:
public SftpConnection(string host, string username, string password = "", int port = 22)
This has multiple advantages:
Need only one XML comment. The whole point of my answer. ☺
Users of Visual Studio can instantly see that the default value for port
is 22. With the overloads, this is not obvious; you have to specifically mention it in the documentation.
You indirectly encourage client code to become more readable by encouraging the use of named parameters (e.g. port: 2222
instead of just 2222
, which is less clear).
And the greatest part about this is that using default values does not remove the ability to still have several overloads if you need them. Typical examples where you want overloads with default values might be something like...
ReadFrom(string filename, ReaderOptions options = null) ReadFrom(Stream stream, ReaderOptions options = null) ReadFrom(byte[] rawData, ReaderOptions options = null)
In these cases, I would argue the XML comments should actually be different.
InheritDoc works perfectly for overloads (at least in VS 2019). You can override any part of it too. Official documentation says:
Inherit XML comments from base classes, interfaces, and similar methods.
/// <summary> /// Method does something /// </summary> /// <param name="someString">Some string</param> public void SomeMethod(string someString) { } /// <param name="someInt">Some int</param> /// <inheritdoc cref="SomeMethod(string)"/> public void SomeMethod(string someString, int someInt) { } /// <summary>Override the summary part</summary> /// <param name="someString">Description for someString overridden</param> /// <param name="anotherInt">Another int</param> /// <inheritdoc cref="SomeMethod(string, int)"/> public void SomeMethod(string someString, int someInt, int anotherInt) { } /// <typeparam name="TOtherType">Other type</typeparam> /// <inheritdoc cref="IInterface{TModel,TKey}.SomeMethod{TType}(TType)"/> public void SomeMethod<TType, TOtherType>(TType first, TOtherType second) { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With