Search⌘ K
AI Features

Documenting Function with XML Comments and Lambda Usage

Explore how to enhance C# function documentation using XML comments for better tooltips and generate clear documentation. Learn functional programming concepts with lambdas by comparing imperative and declarative styles using the Fibonacci sequence example. Gain skills in writing maintainable, modular, and expressive code in C#.

By default, when calling a function such as CardinalToOrdinal, code editors will show a tooltip with basic information, as shown in the figure:

 A tooltip showing the default simple method signature
A tooltip showing the default simple method signature

Improving tooltip

Let’s improve the tooltip by adding extra information:

Step 1: If you are using Visual Studio Code with the C# extension, navigate to “View | Command Palette | Preferences”: “Open Settings (UI)”, and then search for formatOnType and ensure it is enabled. C# XML documentation comments are a built-in feature of Visual Studio 2022.

Step 2: On the line above the CardinalToOrdinal function, type three forward slashes ///, and note that they are expanded into an XML comment that recognizes that the function has a single parameter named number, as shown in the following code:

XML
/// <summary>
///
/// </summary>
/// <param name="number"></param>
/// <returns></returns>

Step 3: Enter suitable information for the XML documentation comment for the CardinalToOrdinal function. Add a summary, and describe the input parameter and the return value, as shown highlighted in the following code:

XML
/// <summary>
/// Pass a 32-bit integer and it will be converted into its ordinal equivalent.
/// </summary>
/// <param name="number"> Number as a cardinal value e.g. 1, 2, 3, and so on.</param>
/// <returns>Number as an ordinal value e.g. 1st, 2nd, 3rd, and so on.</returns>

Step 4: Now, when calling the function, we will see more details, as shown in ...