I have noticed two distinct methods of namespace declaration in C#.
namespace FirstProgram;
and
namespace FirstProgram {...}
I am looking for information on the key distinctions and purposes of the two types of namespace declarations in C#.
Example: with a semicolon ";"
namespace FirstProgram; // Why use semecolon (;)?
class Program
{
//fields and methods
}
and
Example: with curly braces "{...}"
namespace FirstProgram // What is the difference in using curly braces ({...})?
{
class Program
{
//fields and methods
}
}
Namespaces without the curly braces are called file-scoped namespaces
and were introduced with C# 10 . Usually, a C# file contains only a single namespace. If the File-scoped namespaces
are used, one level of nesting can be eliminated.
The only difference is, that when using file-scoped namespaces
, you can't have multiple namespaces inside a single file.
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.