創(chuàng)建C#項目且使用.Net6.0以上的版本時,默認(rèn)code會使用頂級語句形式:
1、略去static void Main(String[ ] args)主方法入口;
2、隱式使用(即隱藏且根據(jù)代碼所需要的類自動調(diào)用)其他命名空間(包括):
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
如果想精確的控制使用的命名空間可通過下述關(guān)閉隱式使用(implicitUsing):
1、選中項目,右鍵選擇《編輯項目文件》,默認(rèn)模板如下:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
2、在<PropertyGroup>標(biāo)簽內(nèi)添加下述標(biāo)簽后保存即可關(guān)閉隱式使用:
<ImplicitUsings>disable</ImplicitUsings>
?文章來源:http://www.zghlxwxcb.cn/news/detail-712229.html
其他問題:
在C#10之后,每個文件只有一個命名空間時,可以使用簡易命名空間定義,即:
原namespace形式為
namespace xxx
{
coding section
}
可修改為
namespace xxx;
coding section
?
?。?!注意,簡易命名空間需要在所有的類型定義之前,所以會和頂級語句產(chǎn)生沖突。
?
參考地址:.NET 6+ 中的 C# 控制臺應(yīng)用模板更改 - .NET | Microsoft Learn文章來源地址http://www.zghlxwxcb.cn/news/detail-712229.html
到了這里,關(guān)于【C#】【命名空間(namespace)】.NET6.0后支持的頂級語句使用問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!