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#.
We'll cover the following...
By default, when calling a function such as CardinalToOrdinal, code editors will show a tooltip with basic information, as shown in the figure:
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:
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:
Step 4: Now, when calling the function, we will see more details, as shown in ...