Ý loài kiến phản hồi sẽ được gửi cho cultureldjazair2007.com: bằng phương pháp nhấn nút gửi, chủ kiến phản hồi của các bạn sẽ được áp dụng để nâng cấp các sản phẩm và dịch vụ của cultureldjazair2007.com. Chế độ về quyền riêng tư.
Bạn đang xem: Namespace c# là gì
Trong nội dung bài viết này
The namespace từ khóa is used to declare a scope that contains a phối of related objects. You can use a namespace to organize code elements & to create globally quality types.
namespace SampleNamespace class SampleClass interface ISampleInterface struct SampleStruct enum SampleEnum a, b delegate void SampleDelegate(int i); namespace Nested class SampleClass2 File scoped namespace declarations enable you lớn declare that all types in a tệp tin are in a single namespace. Tệp tin scoped namespace declarations are available with C# 10. The following example is similar lớn the previous example, but uses a file scoped namespace declaration:
using System;namespace SampleFileScopedNamespace;class SampleClass interface ISampleInterface struct SampleStruct enum SampleEnum a, b delegate void SampleDelegate(int i);The preceding example doesn"t include a nested namespace. Tệp tin scoped namespaces can"t include additional namespace declarations. You cannot declare a nested namespace or a second file-scoped namespace:
namespace SampleNamespace;class AnotherSampleClass public void AnotherSampleMethod() System.Console.WriteLine( "SampleMethod inside SampleNamespace"); namespace AnotherNamespace; // Not allowed!namespace ANestedNamespace // Not allowed! // declarations...Within a namespace, you can declare zero or more of the following types:
nested namespaces can be declared except in tệp tin scoped namespace declarationsThe compiler adds a mặc định namespace. This unnamed namespace, sometimes referred lớn as the global namespace, is present in every file. It contains declarations not included in a declared namespace. Any identifier in the global namespace is available for use in a named namespace.
Namespaces implicitly have public access. For a discussion of the access modifiers you can assign khổng lồ elements in a namespace, see Access Modifiers.
It"s possible to define a namespace in two or more declarations. For example, the following example defines two classes as part of the MyCompany namespace:
namespace MyCompany.Proj1 class MyClass namespace MyCompany.Proj1 class MyClass1 The following example shows how to call a static method in a nested namespace.
Xem thêm: Tiếng Thiên Lý Mã Là Gì, Thiên Lý Mã Là Gì, Thiên Lý Mã Nghĩa Là Gì
namespace SomeNameSpace public class MyClass static void Main() Nested.NestedNameSpaceClass.SayHello(); // a nested namespace namespace Nested public class NestedNameSpaceClass public static void SayHello() Console.WriteLine("Hello"); // Output: Hello