

Introducing C# 10: File-scoped namespaces
source link: https://anthonygiretti.com/2021/08/12/introducing-c-10-file-scoped-namespaces/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introducing C# 10: File-scoped namespaces
Introduction
C# is still evolving and C# 10 proposes a new feature : file_scoped namespaces. As its name suggests, the namespace declared in a file (without braces, but whose instruction ends with a semicolon) will apply to all elements declared in the same file. It’s practical, no more need for indentation since the opening and closing braces of the namespace disappear. There is still a limitation, only one namespace can be declared in the file, otherwise you will get a compiler error.
Example
The following code shows what is a file-scoped namespace:
namespace DemoCsharp10.Products;
public record struct Product { public string Name { get; init; } public int CategoryId { get; init; } public ProductWarranty Warranty { get; init; } }
public enum ProductWarranty { Unlimited = 0, OneYear = 1, TwoYears = 2 }
The following below is illegal:
namespace DemoCsharp10;
namespace Products //Error CS8955 Source file can not contain both file-scoped and normal namespace declarations. { public record struct Product { public string Name { get; init; } public int CategoryId { get; init; } public ProductWarranty Warranty { get; init; } }
public enum ProductWarranty { Unlimited = 0, OneYear = 1, TwoYears = 2 } }
The compiler will throw the following error:
Error CS8955 Source file can not contain both file-scoped and normal namespace declarations.
The following below is also illegal:
namespace DemoCsharp10; namespace Products; //Error CS8954 Source file can only contain one file-scoped namespace declaration.
public record struct Product { public string Name { get; init; } public int CategoryId { get; init; } public ProductWarranty Warranty { get; init; } }
public enum ProductWarranty { Unlimited = 0, OneYear = 1, TwoYears = 2 }
The compiler will throw the following error:
Error CS8954 Source file can only contain one file-scoped namespace declaration.
Like this:
Recommend
-
200
本文发布在我的博客vue中慎用style的scoped属性许可协议: 署名-非商业性使用-禁止演绎4.0国际 转载请保留原文链接及作者。 在vue组件中,在style标签上添加scoped属性,以表示它的样式作用于当下的模块,很好的实现了样式私有化
-
63
PHP RFC: Namespace-scoped declares Version: 0.9 Date: 2016-09-20 Author: Nikita Popov
-
77
-
50
前言 文章原文地址:Nealyang/PersonalBlog 可能作为一个前端,在学习 Flutter 的过程中,总感觉非常非常相似 React Native,甚至于,其中还是...
-
61
Scoped Styles for React :warning: EARLY VERSION Get your CSS classes scoped by component directory Installation npm i react-scoped-styles
-
5
C# 10.0 and .NET 6.0 will be released in November 2021. Time to look at the new language features with a little series of blog posts. Let’s start in this blog post with a very simple feature that is called
-
9
-
32
.NET Tools How-To's File-Scoped Namespaces – A Look at New Language Features in C#...
-
8
C# 11 File Scoped Types
-
9
File Scoped Types in C# 11 Since the evaluation of C#, we have seen many access modifiers introduced, and similarly, in C# 11, file-scoped type is a new access modifier mainly designed for code generator authors where they can create a t...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK