c# 命名空間命名規范
C#命名空間 (C# Namespace )
In C# namespaces are used to group similar type of classes. Two classes with same name in different namespaces never conflict to each other.
在C#中,名稱空間用于對相似類型的類進行分組。 在不同名稱空間中具有相同名稱的兩個類永遠不會相互沖突。
In C# namespace can be:
在C#中,命名空間可以是:
User defined
用戶自定義
Pre defined, that is in-built in .NET class library
預定義,內置在.NET類庫中
Here, we need to use using keyword to access defined namespaces.
在這里,我們需要使用using關鍵字來訪問已定義的名稱空間。
Syntax:
句法:
namespace <namespace_name>
{
//Write code here
}
Note:
注意:
To declare user defined namespace we need to use namespace keyword.
要聲明用戶定義的名稱空間,我們需要使用namespace關鍵字。
If we want to access class defined inside namespace then we need use . (dot) Operator.
如果要訪問命名空間中定義的類,則需要使用。 (點)運算符。
Example:
例:
using System;
using System.Collections;
namespace namespace1
{
class ABC
{
public void fun()
{
Console.WriteLine("Inside Namespace1");
}
}
}
namespace namespace2
{
class ABC
{
public void fun()
{
Console.WriteLine("Inside Namespace2");
}
}
}
class Program
{
static void Main()
{
namespace1.ABC OB1 = new namespace1.ABC();
namespace2.ABC OB2 = new namespace2.ABC();
OB1.fun();
OB2.fun();
}
}
Output
輸出量
Inside Namespace1
Inside Namespace2
Read more: The 'using' Keyword in C#, Nested Namespace in C#
: C#中的“ using”關鍵字,C#中的嵌套命名空間
翻譯自: https://www.includehelp.com/dot-net/namespaces-in-c-sharp.aspx
c# 命名空間命名規范