简体   繁体   中英

C# Visual Studio 2019 red squigglies for syntax error not visible when “Open a local folder” file is opened

I've found that if I open the same C# file in Visual Studio 2019 v16.9.3 in different ways, I can get the red squigglies in the text editor or not.

I am following a tutorial (PluralSight, C# Fundamentals) and I have created a directory that is structured as follows:

  • gradebook/
    • src/
      • GradeBook/
        • GradeBook.csproj
        • Program.cs
    • test/

The tutorial instructs me to open the folder using the "Open a local folder" option at the Visual Studio home screen. However, when I do this, there are no red squigglies for errors in my code. However, if I open the project by Ctrl+Shift+O and selecting the "GradeBook.csproj" file listed above, everything seems right with the world.

To clarify, I have included a few screenshots below:

File opened with: Open a local folder

File opened with: Open a project/solution

In case of image problems, the 2 images show a picture of the same Program.cs file opened via the two methods mentioned above. The code in the file is as follows:

using System;

namespace Gradebook
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            int x = "L";
            x.grade();
        }
    }
}

The first picture (file opened via "Open a local folder") shows no red squigglies under the two syntactically incorrect lines, while the second picture (opened via Ctrl+Shift+O) shows red squigglies.

Does anybody know how I might go about having the entire parent folder opened in the Solution Explorer while also being able to see the red squigglies?

As a final note: I have also noticed that, when the file is opened via the local folder, typing opening/closing braces ("{") doesn't autocomplete as it does when opening the file via the project/solution.

This happens because when you open as a local folder you're only browsing the files, Visual Studio is not actually loading your project. When you open the.csproj file Visual Studio knows you're opening a project and loads everything correctly. It's weird that the tutorial you're following didn't provide a proper solution file with the project and extra files inside.

To open as you want, the parent folder, you need to create an empty solution project somewhere, then paste the files in the folder, on Solution Explorer (inside VS) you click to Show All Files (otherwise your pasted files won't show) then add these files to your solution (ricght click, add). Then add add the csproj as an existing project (right click on solution, add existing project, VS will let you select it once you try to add it).

Or use VSCode as you did, VSCode is different indeed from Visual Studio:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM