Guide to the Language
Download 2 Mb. Pdf ko'rish
|
C sharp
- Bu sahifa navigatsiya:
- Namespace Access
- Using Directive
Nested Namespaces
Namespaces can be nested any number of levels deep to further define the namespace hierarchy. namespace MyNamespace { namespace NestedNamespace { class MyClass {} } } 114 A more concise way to write this is to separate the namespaces with a dot. namespace MyNamespace.NestedNamespace { class MyClass {} } Note that declaring the same namespace again in another class within the project has the same effect as if both namespaces were included in the same block, even if the class is located in another source code file. Namespace Access To access a class from another namespace, you need to specify its fully qualified name. namespace MyNamespace.NestedNamespace { public class MyClass {} } namespace OtherNamespace { class MyApp { static void Main() { MyNamespace.NestedNamespace.MyClass myClass; } } } Chapter 19 NamespaCes 115 Using Directive The fully qualified name can be shortened by including the namespace with a using directive. The members of that namespace can then be accessed anywhere in the code file without having to prepend the namespace to every reference. It is mandatory to place using directives before all other members in the code file. using MyNamespace.NestedNamespace; Having direct access to these members means that if there is a conflicting member signature in the current namespace, the member in the included namespace will be hidden. For example, if there is a MyClass in the OtherNamespace as well, that class will be used by default. To use the class in the included namespace, the fully qualified name would again have to be specified. using MyNamespace.NestedNamespace; namespace MyNamespace.NestedNamespace { public class MyClass { public static int x; } } namespace OtherNamespace { public class MyClass { static void Main() { Chapter 19 NamespaCes 116 int x = MyNamespace.NestedNamespace.MyClass.x; } } } To simplify this reference, the using directive can instead be changed to assign the namespace to an alias. using MyAlias = MyNamespace.NestedNamespace; // ... int x = MyAlias.MyClass.x; An even shorter way would be to define the fully qualified class name as a new type for the code file, by using the same alias notation. using MyType = MyNamespace.NestedNamespace.MyClass; // ... int x = MyType.x; A using static directive was added in C# 6.0. This directive imports only the accessible static members of the type into the current namespace. In the following example, static members of the Math class can be used without qualification due to the using static directive. using static System.Math; public class Circle { public double radius { get; set; } public double Area { get { return PI * Pow(radius, 2); } } } Chapter 19 NamespaCes 117 © Mikael Olsson 2020 M. Olsson, C# 8 Quick Syntax Reference, https://doi.org/10.1007/978-1-4842-5577-3_20 Download 2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling