![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Using Namespaces
Số trang: 3
Loại file: pdf
Dung lượng: 12.59 KB
Lượt xem: 8
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Sử dụng Namespaces Ví dụ bạn có thấy cho đến nay là một chương trình rất nhỏ. Tuy nhiên, các chương trình nhỏ sớm có thể phát triển thành các chương trình lớn hơn. Theo một chương trình phát triển, nó tạo ra hai vấn đề. Đầu tiên, thêm mã là khó hiểu và duy trì hơn so với mã ít hơn.
Nội dung trích xuất từ tài liệu:
Using NamespacesUsing NamespacesThe example you have seen so far is a very small program. However, small programs cansoon grow into bigger programs. As a program grows, it creates two problems. First,more code is harder to understand and maintain than less code. Second, more codeusually means more names; more named data, more named methods, and more namedclasses. As the number of names increases so does the likelihood of the project buildfailing because two or more names clash (especially when the program uses third-partylibraries).In the past, programmers tried to solve the name-clashing problem by prefixing nameswith some sort of qualifier (or set of qualifiers). This solution is not a good one becauseits not scalable; names become longer and you spend less time writing software andmore time typing (there is a difference) and reading and re-reading incomprehensiblylong names.Namespaces help solve this problem by creating a named container for other identifiers,such as classes. Two classes with the same name will not be confused with each other ifthey live in different namespaces. You can create a class named Greeting inside thenamespace named TextHello, like this:namespace TextHello{ class Greeting { ... }}You can then refer to the Greeting class as TextHello.Greeting in your own programs. Ifsomeone else also creates a Greeting class in a different namespace and installs it on yourcomputer, your programs will still work as expected because they are using theTextHello.Greeting class. If you want to refer the new Greeting class, you must specifythat you want the class from the new namespace.It is good practice to define all your classes in namespaces, and the Visual Studio 2005environment follows this recommendation by using the name of your project as the top-level namespace. The .NET Framework Software Developer Kit (SDK) also adheres tothis recommendation; every class in the .NET Framework lives inside a namespace. Forexample, the Console class lives inside the System namespace. This means that its fullyqualified name is actually System.Console.Of course, if you had to write the fully qualified name of a class every time, it would beno better that just naming the class SystemConsole. Fortunately, you can solve thisproblem with a using directive. If you return to the TextHello program in Visual Studio2005 and look at the file Program.cs in the Code and Text Editor window, you will noticethe following statements:using System;using System.Collections.Generic;using System.Text;The using statement brings a namespace into scope, and you no longer have to explictlyqualify objects with the namespace they belong to in the code that follows. The threenamespaces shown contain classes that are used so often that Visual Studio 2005automatically adds these using statements every time you create a new project. You canadd further using directives to the top of a source file.The following exercise demonstrates the concept of namespaces further.Try longhand names 1. In the Code And Text Editor window, comment out the using directive at the top of Program.cs: //using System; 2. On the Build menu, click Build Solution. The build fails, and the Output pane displays the following error message twice (once for each use of the Console class): The name Console does not exist in the current context. 3. In the Output pane, double-click the error message. The identifier that caused the error is selected in the Program.cs source file. TIP The first error can affect the reliability of subsequent diagnostic messages. If your build has more than one diagnostic message, correct only the first one, ignore all the others, and then rebuild. This strategy works best if you keep your source files small and work iteratively, building frequently. 4. In the Code and Text Editor window, edit the Main method to use the fully qualified name System.Console. Main should look like this: static void Main(string[] args) { System.Console.WriteLine(Hello World); } NOTE When you type System., notice how the names of all the items in the System namespace are displayed by IntelliSense. 5. On the Build menu, click Build Solution. The build succeeds this time. If it doesnt, make sure Main is exactly as it appears in the preceding code, and then try building again. 6. Run the application to make sure it still works by clicking Start Without Debugging on the Debug menu.In the Solution Explorer, click the + to the left of the References entry. This displays theassemblies referenced by the Solution Explorer. An assembly is a library containing codewritten by other developers (such as the .NET Framework). In some cases, the classes ina namespace are stored in an assembly that h ...
Nội dung trích xuất từ tài liệu:
Using NamespacesUsing NamespacesThe example you have seen so far is a very small program. However, small programs cansoon grow into bigger programs. As a program grows, it creates two problems. First,more code is harder to understand and maintain than less code. Second, more codeusually means more names; more named data, more named methods, and more namedclasses. As the number of names increases so does the likelihood of the project buildfailing because two or more names clash (especially when the program uses third-partylibraries).In the past, programmers tried to solve the name-clashing problem by prefixing nameswith some sort of qualifier (or set of qualifiers). This solution is not a good one becauseits not scalable; names become longer and you spend less time writing software andmore time typing (there is a difference) and reading and re-reading incomprehensiblylong names.Namespaces help solve this problem by creating a named container for other identifiers,such as classes. Two classes with the same name will not be confused with each other ifthey live in different namespaces. You can create a class named Greeting inside thenamespace named TextHello, like this:namespace TextHello{ class Greeting { ... }}You can then refer to the Greeting class as TextHello.Greeting in your own programs. Ifsomeone else also creates a Greeting class in a different namespace and installs it on yourcomputer, your programs will still work as expected because they are using theTextHello.Greeting class. If you want to refer the new Greeting class, you must specifythat you want the class from the new namespace.It is good practice to define all your classes in namespaces, and the Visual Studio 2005environment follows this recommendation by using the name of your project as the top-level namespace. The .NET Framework Software Developer Kit (SDK) also adheres tothis recommendation; every class in the .NET Framework lives inside a namespace. Forexample, the Console class lives inside the System namespace. This means that its fullyqualified name is actually System.Console.Of course, if you had to write the fully qualified name of a class every time, it would beno better that just naming the class SystemConsole. Fortunately, you can solve thisproblem with a using directive. If you return to the TextHello program in Visual Studio2005 and look at the file Program.cs in the Code and Text Editor window, you will noticethe following statements:using System;using System.Collections.Generic;using System.Text;The using statement brings a namespace into scope, and you no longer have to explictlyqualify objects with the namespace they belong to in the code that follows. The threenamespaces shown contain classes that are used so often that Visual Studio 2005automatically adds these using statements every time you create a new project. You canadd further using directives to the top of a source file.The following exercise demonstrates the concept of namespaces further.Try longhand names 1. In the Code And Text Editor window, comment out the using directive at the top of Program.cs: //using System; 2. On the Build menu, click Build Solution. The build fails, and the Output pane displays the following error message twice (once for each use of the Console class): The name Console does not exist in the current context. 3. In the Output pane, double-click the error message. The identifier that caused the error is selected in the Program.cs source file. TIP The first error can affect the reliability of subsequent diagnostic messages. If your build has more than one diagnostic message, correct only the first one, ignore all the others, and then rebuild. This strategy works best if you keep your source files small and work iteratively, building frequently. 4. In the Code and Text Editor window, edit the Main method to use the fully qualified name System.Console. Main should look like this: static void Main(string[] args) { System.Console.WriteLine(Hello World); } NOTE When you type System., notice how the names of all the items in the System namespace are displayed by IntelliSense. 5. On the Build menu, click Build Solution. The build succeeds this time. If it doesnt, make sure Main is exactly as it appears in the preceding code, and then try building again. 6. Run the application to make sure it still works by clicking Start Without Debugging on the Debug menu.In the Solution Explorer, click the + to the left of the References entry. This displays theassemblies referenced by the Solution Explorer. An assembly is a library containing codewritten by other developers (such as the .NET Framework). In some cases, the classes ina namespace are stored in an assembly that h ...
Tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 282 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 279 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 275 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 235 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 231 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 223 0 0 -
Giáo án Tin học lớp 11 (Trọn bộ cả năm)
125 trang 219 1 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 194 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 176 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 170 0 0