As other answers have already explained in length, you can enable line numbers by going into Tools -> Options ... clickety clickety click ... arriving at some page with an insanely large number of irrelevant options, scanning through them, finding a "Line Numbers" checkbox, and checking it. If you want to disable line numbers, it is clickety clickety click all over again.
Still as of 2024, Visual Studio does not offer a toolbar button or keyboard shortcut for toggling line numbers on or off.
This is particularly problematic since line numbers take up a non-negligible amount of screen real estate, which does not justify their (extremely limited) usefulness, so it is best to keep them hidden most of the time. Unfortunately, sometimes you need them, and then it is a pain to toggle them on and off.
The good news: you do not actually need line numbers.
Most people who have line numbers visible at all times do it due to one of the following reasons:
They are insecure without them. They almost never look at the actual numbers, but are used to them always being there, so if they are not there, they feel something is missing. (Same syndrome as editor tabs: they are useless, they waste screen real estate, and they require valuable time to keep curating them, but people feel they need them.)
They are using lame tooling which does not support double-click-to-go-to-source-line. For example, aspx web sites that show diagnostic stack traces in html. (Instead of logging the stack trace in the output window, using the same format as the compiler uses for error messages, so you can just double-click the log line in the output window and have visual studio show you the source code.)
How to live your life without line numbers:
Make sure that absolutely all of your logging goes to the output window of visual studio, and that it is in the exact same format as the compiler uses for error messages, so that each line in the output window can be double-clicked to take you to the right source file and line number. You should never be in a situation where you have to read a source filename and line number from the screen and manually find that source file and that line number.
In the exceedingly rare event that you do, nonetheless, have to find a particular source line by number, use the "Go to line" command. By default, this command is bound to Ctrl+G
.
If you want to know which line you are at, either look at the information shown to the right of the horizontal scrollbar at the bottom of the text editor window, or press Ctrl+G
again: the "Go to line" dialog has the current line already filled in.