.NET 6 Hello World Console应用程序中的错误

发布于 2025-01-26 23:34:02 字数 1423 浏览 3 评论 0原文

遵循 Microsoft 用于在 .NET 6.0 中设置新的控制台应用程序,我立即从Intellisense获得语法错误。但是,当我进行dotnet运行时,程序将按预期运行。 怪异

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

这是文件夹示例代码 terminal

  • ​> program :未定义或导入[helloworld] void:预定义的类型
  • system.void'未定义或导入[helloworld]
  • ' :预定义的类型'system.string'未定义或导入[helloworld]
  • console:名称'console'在当前上下文[helloworld]

.net sdks 中不存在安装

  • 5.0.201 [C:\ Program Files \ dotnet \ sdk]
  • 6.0.102 [C:\ Program Files \ dotnet \ sdk]

helloworld.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

相关VS代码扩展:

  1. C#V1.24.0

After following the steps from Microsoft for setting up a new Console Application in .NET 6.0, I immediately get syntax errors from Intellisense. However, when I do dotnet run, the program runs as expected. weird

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Here's a screenshot of the folder, sample code and terminal

enter image description here

  • Program: Predefined type 'System.Object' is not defined or imported [HelloWorld]
  • Void: Predefined type 'System.Void' is not defined or imported [HelloWorld]
  • string: Predefined type 'System.String' is not defined or imported [HelloWorld]
  • Console: The name 'Console' does not exist in the current context [HelloWorld]

.NET SDKs installed

  • 5.0.201 [C:\Program Files\dotnet\sdk]
  • 6.0.102 [C:\Program Files\dotnet\sdk]

HelloWorld.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

Relevant VS Code Extensions:

  1. C# v1.24.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神回复 2025-02-02 23:34:02

对我来说,当我删除“ .vs”隐藏文件夹时工作。此文件夹位于“ .sln”文件的同一位置

For me, worked when i deleted the ".vs" hidden folder. this folder was in the same location of ".sln" file

∞觅青森が 2025-02-02 23:34:02

删除.vs为我工作。

问题似乎是在MS Windows(10 Pro)更新之后开始的。

当试图显示项目属性时,问题还伴随着Vs 2022(17.2.6)可回收异常(请参见下文)。

尽管如此,当重建解决方案中的所有项目(其中12个)时,没有错误,代码运行良好 - 但是编辑器显示了很多错误(但在所有文件或项目中却没有 - 怪异)。

例外:

=================================
7/20/2022 11:31:27 AM
可恢复
Microsoft.visualstudio.projectSystem.Query.QueryExecutionException:对象引用未设置为对象实例。 ---&gt; System.NullReferenceException:对象引用未设置为对象的实例。
在Microsoft.visualstudio.languageservices.csharp.projectsystemsshim.entrypointfinder.findentrypoints(inamespaceSymbol符号)
在Microsoft.visualstudio.projectsystem.properties.startupobjectsenumgenerator.d__8.movenext()
---堆栈跟踪的结尾是从上面抛出例外的位置------
在system.runtime.exceptionservices.exceptiondispatchinfo.throw()
在Microsoft.visualstudio.projectsystem.propertypages.pagedyynamicenumproperty.d__6.movenext()()
---堆栈跟踪的结尾是从上面抛出例外的位置------
在system.runtime.exceptionservices.exceptiondispatchinfo.throw()
在system.runtime.com.pilerservices.taskawaiter.handlenonsuccessanddebuggernotification(任务任务)
在Microsoft.visualstudio.projectsystem.vs.query.supportedvaluedataproducer.d__1.movenext()()
---堆栈跟踪的结尾是从上面抛出例外的位置------
在system.runtime.exceptionservices.exceptiondispatchinfo.throw()
在system.runtime.com.pilerservices.taskawaiter.handlenonsuccessanddebuggernotification(任务任务)
在Microsoft.visualStudio.projectSystem.VS.Query.QueryDataFromProvidEstateProducerBase 1.&lt; sendRequestAsync&lt; d__0.movenext()------------- 1.d__9.movenext()
---堆栈跟踪的结尾是从上面抛出例外的位置------
在system.runtime.exceptionservices.exceptiondispatchinfo.throw()
在system.runtime.com.pilerservices.taskawaiter.handlenonsuccessanddebuggernotification(任务任务)
在Microsoft.visualStudio.projectSystem.Query.QueryExecution.QueryExecution.QuerySubscription 1.&lt; c__displayClass8_0。 ---在system.runtime.exceptionservices.exceptiondispatchinfo.throw() &lt; initizeAsync&gt; d__10.movenext()----堆栈跟踪的结尾从先前的位置抛出---- system.runtime.exceptionservices.exceptiondispatchinfo.throw(system.runtime.runtime.compilime.compilers.pilererser.compilerers.pilererser.taskawaiter.taskawaiter.handlenonsuccessanddebuggernotifification(toc)在system.runtime.compilerservices.taskawaiter 1.getResult()()
在microsoft.visualstudio.projectsystem.vs.implementation.propertypages.designer.projectProperpertyDataAccess.Observer.d__99.movenext()
---堆栈跟踪的结尾是从上面抛出例外的位置------
在system.runtime.exceptionservices.exceptiondispatchinfo.throw()
在system.runtime.com.pilerservices.taskawaiter.handlenonsuccessanddebuggernotification(任务任务)
在Microsoft.visualStudio.projectSystem.vs.implementation.propertypages.designer.projectPropertiesEditor。
---堆栈跟踪的结尾是从上面抛出例外的位置------
在system.runtime.exceptionservices.exceptiondispatchinfo.throw()
在system.runtime.com.pilerservices.taskawaiter.handlenonsuccessanddebuggernotification(任务任务)
在Microsoft.visualstudio.projectsystem.vs.implementation.propertypages.designer.asyncloadcontent。

Deleting .vs worked for me.

Problem seemed to start after an MS Windows (10 pro) update.

Problem also accompanied by a VS 2022 (17.2.6) recoverable exception when trying to display a project properties (see below).

For all its worth, when rebuilding all projects in the solution (12 of them) compile with no errors and the code runs just fine - yet the editor shows tons of errors (but not in all files or projects - weird).

Call stack for the exception:

=====================
7/20/2022 11:31:27 AM
Recoverable
Microsoft.VisualStudio.ProjectSystem.Query.QueryExecutionException: Object reference not set to an instance of an object. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.VisualStudio.LanguageServices.CSharp.ProjectSystemShim.EntryPointFinder.FindEntryPoints(INamespaceSymbol symbol)
at Microsoft.VisualStudio.ProjectSystem.Properties.StartupObjectsEnumGenerator.d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.VisualStudio.ProjectSystem.PropertyPages.PageDynamicEnumProperty.d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.ProjectSystem.VS.Query.SupportedValueDataProducer.d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.ProjectSystem.VS.Query.QueryDataFromProviderStateProducerBase1.<SendRequestAsync>d__0.MoveNext() --- End of inner exception stack trace --- at Microsoft.VisualStudio.ProjectSystem.Query.QueryExecution.QuerySubscription1.d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.ProjectSystem.Query.QueryExecution.QuerySubscription1.<>c__DisplayClass8_0.<<Start>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.ProjectPropertyDataAccess.Observer.<InitializeAsync>d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.ProjectPropertyDataAccess.Observer.d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.ProjectPropertiesEditor.<>c__DisplayClass0_0.<<-ctor>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.AsyncLoadContent.<>c__DisplayClass0_0.<b__0>d.MoveNext()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文