简体   繁体   中英

Visual Studio .net standard library template breaks using directive?

I'm currently trying to learn a bit about .NET and get back into programming with C#. To do both I'm following this official tutorial on how to create a .NET standard class library.

One of the first steps is to create a Class Library (.NET Standard) template provided by the .NET kit in visual studio. This creates a basic file with the following code:

using System;

namespace StringLibrary
{
    public class Class1
    {
    }
}

Now visual studio immediately greets me with this error message:

Error CS0246 The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)

I tried google, some people have vaguely similar problems, where they forget to add the reverence to their different projects, however I cannot find any way to add System ... Besides, the rest of the tutorial files work fine and are able to import System . (I can also reference the other files just fine)

Did anyone encounter a similar problem? Is the template bugged, or did I mess up installing the kit (which I did via the Visual Studio installer, so no idea, how that could happen)?

If I can clarify anything, feel free to ask, and thanks in advance.

As @Lex Li suggested, i used dotnet restore in the project folder and got the error NU1101, which meant, that the package I want to use could not be found. I found this website https://www.hossambarakat.net/2020/06/24/fix-error-NU1101/ and chose option A: to add a nuget.config. To do this I opened the command prompt and navigated to \Users\myUser\Appdata\Roaming\NuGet and executed NuGet.Config, which opened a file in Visual Studio. Here I only added the package sources lines from the following lines, since the rest was already there (see also the link):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

After that restarting Visual Studio solved the issue, and I can now continue the tutorial.

Thanks for the help!

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