gpt4 book ai didi

c# - 如何在 vs 代码中生成具有文件范围命名空间语法(C#10)的类?

转载 作者:行者123 更新时间:2023-12-05 00:51:53 24 4
gpt4 key购买 nike

我想按照最新的 C#10 文件范围在 Microsoft Visual Studio Code 中生成一个新的 C# 类或 C# 接口(interface)命名空间语法。

https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/namespaces

Beginning with C# 10, you can declare a namespace for all typesdefined in that file, as shown in the following example:

namespace SampleNamespace;

class AnotherSampleClass
{
public void AnotherSampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside SampleNamespace");
}
}

我正在以这种方式生成 C# 类:

在资源管理器中右键单击文件夹 -> 新建 C# 类。

输出如下所示:(带有大括号的旧语法)

namespace SampleNamespace
{
class SampleClass
{
public void SampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside SampleNamespace");
}
}
}

我正在使用 C# for Visual Studio Code(由 OmniSharp 提供支持)。 v1.24.0

VS Code 版本为 1.62.3

有没有办法覆盖生成器行为以生成新的文件范围命名空间语法?

最佳答案

对不起,这是一个迟到的回应,但我自己刚刚经历过。

您正在寻找的是 Visual Studio 设置。

转到:工具 > 选项 > 文本编辑器 > C# > 代码样式

第 4 block ,“代码块首选项”

将“命名空间声明”从“ block 范围”更改为“文件范围”

(这是在 Visual Studio 2022 中)

关于c# - 如何在 vs 代码中生成具有文件范围命名空间语法(C#10)的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70951439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com