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
}
}
CodePudding user response:
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.
CodePudding user response:
The form without the braces, "file scoped namespace declarations", is a syntax sugar feature introduced in C# 10. They are equivalent.