All posts

· 11 min read

Path · Ch. 20

How to read documentation: find the answer yourself

We learn how the .NET documentation on learn.microsoft.com is organized and how to read an API reference page: signature, parameters, return value, exceptions and examples. We use List<T>.Add and int.TryParse as real cases.

On this page
  1. Three kinds of documentation
  2. Anatomy of an API page: List<T>.Add
  3. A page with more in it: Int32.TryParse
  4. Pick the right version
  5. Documentation inside VS Code
  6. A package’s README
  7. Exercise: read a page you don’t know
  8. Suggested solution
  9. Recap
  10. Stuck reading a page?

Every time you don’t know what a method does, you have two options: ask someone, or read what the people who built it wrote. The second one is available at three in the morning, never gets tired of your questions and almost always has the exact answer. The catch is that the first time you open a reference page, it looks like a different language.

In this chapter you will learn how the .NET documentation is organized, how to read an API page section by section using two real cases (List<T>.Add and int.TryParse), and what to do with the shortcuts you already have inside VS Code.

Three kinds of documentation

The .NET documentation lives at learn.microsoft.com/dotnet. Almost everything falls into one of three categories:

Concepts and fundamentals. They explain how something works and why. In the C# guide they sit under “Fundamentals”, with topics like the type system, object-oriented programming and exceptions. You read them to understand.

Tutorials. They walk you step by step through building something. The same C# guide has a “Get started” section with “A tour of C#” and “Beginner C# tutorials”. You follow them to practice.

Reference. It describes every piece precisely and teaches nothing along the way. From the main .NET page there is a link to the ”.NET API reference” (every class and method in the library) and another to the “C# language reference” (keywords and operators). You consult it when you already know what you are looking for.

When you are learning something new, start with a concept or a tutorial. When you have already used it and have a specific question (“what does this return if it fails?”), go to the reference. That specific question is the one you will have most of the time, which is why this chapter focuses on the reference.

Anatomy of an API page: List<T>.Add

Open the page for List<T>.Add. This is the method you used in chapter 5 every time you wrote list.Add(...). From top to bottom, the page has:

Title and description. The title is “List.Add(T) Method” and the short description says “Adds an object to the end of the List”. That already tells you what it does: it appends.

Definition. Facts about where it comes from: the namespace (System.Collections.Generic), the assembly it lives in and a link to the source code. You can ignore it for now, but it is useful to know that the .NET source code is public.

The signature. This is the most important line on the page:

public void Add(T item);

Read it like this: it is public, so you can call it from outside. It returns void, which means nothing: don’t expect a result. It is called Add and takes one parameter, item, of type T. The T is the type of the list’s elements: in a List<decimal>, T is decimal.

Parameters. It explains each parameter. For item it says: “The object to be added to the end of the List. The value can be null for reference types.” So if the list holds reference types, like string, you can add null.

Examples. Complete code examples with their output. The Add page has one with a Part class and another with a list of dinosaurs. They are long because they show several methods at once, and it is fine if you don’t understand all of them. Find the line that uses Add and see how it is called.

Remarks. Important notes that don’t fit elsewhere. Here it says List<T> accepts null for reference types and allows duplicate elements. It also explains that when the list is full, .NET allocates a bigger array and copies the elements over, so Add is normally an O(1) operation and only occasionally O(n). You don’t need to memorize the notation; what matters is that adding sometimes takes longer and the page warns you.

Applies to and See also. “Applies to” summarizes where the method exists. “See also” links to neighbors: AddRange, Insert, Remove and Count. When you can’t find the method you want, the right neighbor is often here.

Notice what is not on this page: there is no “Returns” section, because it returns void, and no “Exceptions” section. A missing section is information too.

A page with more in it: Int32.TryParse

Now open Int32.TryParse. You have used this one since chapter 3 and saw it again in chapter 7. The first thing you will see is an Overloads section: a list of several versions of the same method with different parameters. Some take a ReadOnlySpan<char>, others take an IFormatProvider or a NumberStyles, and then there is the one you use, TryParse(String, Int32). Pick the one that matches what you write in your code.

For that version, the signature is:

public static bool TryParse(string? s, out int result);

The page shows two variants of the signature, with and without the ?. The ? in string? means the text can be null, and it is the one that applies with <Nullable>enable</Nullable>. Again, piece by piece: static means you call it on the type name (int.TryParse(...)) and not on a variable. It returns bool. It takes a string? called s and an out parameter called result.

Parameters. For s it says “A string containing a number to convert.” The description for result is longer and worth reading in full: it holds the number if the conversion worked, or zero if it failed. It says the conversion fails if s is null or empty, is not in the correct format, or represents a number less than Int32.MinValue or greater than Int32.MaxValue. And it adds that the parameter is “passed uninitialized”: any value it had before is overwritten.

Returns. true if s was converted successfully, false otherwise. That is why you can put the call inside an if.

Examples and Remarks. The official example tries a list of strings. The page explains why some fail: "9432.0" fails because the string cannot contain a decimal separator, "16,667" fails because it cannot contain group separators, and "01FA" fails because hexadecimal digits are not accepted. Meanwhile " -322 " and "+4302" do convert. The Remarks section sums up the accepted format as [ws][sign]digits[ws]: optional spaces, an optional sign, digits from 0 to 9 and optional spaces.

With the page open, you can check it yourself:

string[] texts = { "160519", "9432.0", "16,667", "   -322   ", "+4302", "01FA", "" };

foreach (string t in texts)
{
    if (int.TryParse(t, out int number))
    {
        Console.WriteLine($"'{t}' -> {number}");
    }
    else
    {
        Console.WriteLine($"'{t}' could not be converted (result is {number})");
    }
}
'160519' -> 160519
'9432.0' could not be converted (result is 0)
'16,667' could not be converted (result is 0)
'   -322   ' -> -322
'+4302' -> 4302
'01FA' could not be converted (result is 0)
'' could not be converted (result is 0)

You read the documentation, ran the test and the output matches. That is reading documentation well: not just understanding the page, but confirming it by running code.

The Exceptions section deserves a mention. On this page, the only overload that has one is the one that takes NumberStyles. The TryParse(String, Int32) version does not, and that makes sense: TryParse exists precisely to avoid throwing exceptions for badly formed text, as its Remarks section says. Compare with int.Parse, the one that threw the FormatException in chapter 7: open its page and look for an Exceptions section. That section is where you learn what can fail before it fails.

Pick the right version

You are using .NET 10, so you want to read the .NET 10 documentation. Every API page has a version selector. Notice the ?view=net-10.0 parameter I put in this chapter’s links: it changes the version shown. If you leave it out, the page uses its default version, which today is .NET 11 and may show things your version doesn’t have.

This matters more than it seems. A tutorial from five years ago may use a method that no longer exists, or ignore a new one that does the job better. Before copying code from an article, check which .NET version it is for and look at the “Applies to” section.

Documentation inside VS Code

You don’t always need to open the browser. The editor already has documentation at hand, the same kind you saw on the reference pages:

Hover over a method. The tooltip shows the signature and a short description.

Ctrl+Space (Ctrl on Mac too). Opens the suggestion list while you type. According to the VS Code documentation, if you click the > icon in the list, the documentation for the selected item expands to the side and updates as you move through the list.

That text comes from XML documentation comments: three slashes /// above a method. You can write your own, and the tooltip will show them:

Console.WriteLine($"{Calculations.WithDiscount(20.00m, 10):0.00}");

public static class Calculations
{
    /// <summary>Calculates the final price after applying a discount.</summary>
    /// <param name="price">The original price.</param>
    /// <param name="percent">The discount, from 0 to 100.</param>
    /// <returns>The price with the discount applied.</returns>
    public static decimal WithDiscount(decimal price, int percent)
    {
        return price - price * percent / 100;
    }
}
18.00

Type Calculations.WithDiscount( on another line and VS Code shows you the summary and the description of each parameter. It is the same thing the Microsoft team does with List<T>.Add, at a small scale. The C# language reference has a section called “Documentation comments” with all the available tags.

A package’s README

Later you will install NuGet packages, and each one comes with a README. A good README answers, in this order, what the package does, how to install it, a minimal usage example and the license it is distributed under. Before adding a package to your project, read it and run three quick checks: does the example look like what you need, is the project still being updated, and does the license let you use it. If the README has no usage example or the project has been abandoned for years, look at alternatives.

Exercise: read a page you don’t know

Open the page for List<T>.Remove and answer without running anything:

  1. What is the signature? What does it return?
  2. If the list has two equal elements, which one does it remove?
  3. What happens if the item is not in the list? Does it throw an exception?
  4. What comparison does it use to decide that two elements are “equal”?

Then write a small program that confirms it.

Suggested solution

The signature is public bool Remove(T item);. It returns a bool. The description says it removes “the first occurrence” of the object, so with two equal elements it removes only the first. Under Returns, true if it was removed and false otherwise. The page clarifies that it also returns false if the item was not found in the list, so it does not throw an exception. Under Remarks it says it determines equality using EqualityComparer<T>.Default for the type T, and that it performs a linear search, so it is an O(n) operation.

var prices = new List<decimal>();
prices.Add(2.50m);
prices.Add(1.25m);
prices.Add(2.50m);

bool removed = prices.Remove(2.50m);
bool notThere = prices.Remove(9.99m);

Console.WriteLine($"Remove(2.50): {removed}, {prices.Count} left");
Console.WriteLine($"Remove(9.99): {notThere}, {prices.Count} left");
Remove(2.50): True, 2 left
Remove(9.99): False, 2 left

After the first Remove, one 2.50 was left in the list (the second one). And the second call broke nothing: it returned False.

Recap

  • Documentation splits into concepts, tutorials and reference. For specific questions, use the reference.
  • An API page has a signature, parameters, return value, examples, remarks and sometimes exceptions. A missing section tells you something too.
  • A signature reads in pieces: access, return type, name and parameters.
  • Overloads are several versions of the same method; pick the one that matches your call.
  • Use the version selector (?view=net-10.0) to read the docs for the version you have.
  • VS Code shows documentation in tooltips and with Ctrl+Space, and /// comments let you document your own code.
  • Before using a package, read its README and check the example, maintenance and license.

In the next chapter we look at what to do when the documentation is not enough: how to ask for help so you get a fast, good answer.

Stuck reading a page?

DM me on Instagram or ask in the community on Discord. Paste the link to the page and tell us which part wasn’t clear.